PHP in_array function With Examples

The PHP in_array is one of the built-in functions in PHP (Hypertext Preprocessor) which is used to search a specific value on a set of an array.

In this article, we will talk about in_array function in a detailed explanation as well as samples in order for you to understand it easily. This is a continuance of the previous topic, entitled Array Push PHP.

What is in_array PHP?

In PHP, in_array is a function which is used to check if a given value in the array exists or not.

Further, Once the given value exists it will return TRUE, otherwise FALSE.

Syntax:

in_array(search, array, type)

Parameter Values:

ParameterDescription
searchClearly specified what to search into an array (REQUIRED).
arrayIt specifies the elements of an array to search (REQUIRED).
typeOnce the parameter were set to TRUE, then in_array function will search a string and its specific type in the array elements (OPTIONAL).

Technical Details:

Return Value:The return value could return TRUE once the value is already in the array or return FALSE once not.
PHP Version:PHP 4.2 or higher could be the best version – Where the search parameter can be on the array.
PHP Changelog: 4+ version or higher.

First example:

<?php
$people = array("Adones", "Jude", "Paul", "Glenn");

if (in_array("Jude", $people)){
  echo "Name found in the array";
  }
else{
  echo "Name not found in the array";
  }
?>

Output:

Name found in the array

Second example:

<?php
$programmer = array("Jude", "Glenn", "Adones", "Paul", 26);

if (in_array("26", $programmer, TRUE)){
  echo "Match found"."\n";
  }
else{
  echo "Match not found"."\n";
  }
if (in_array("Glenn",$programmer, TRUE)){
  echo "Match found"."\n";
  }
else{
  echo "Match not found"."\n";
  }

if (in_array(26,$programmer, TRUE)){
  echo "Match found"."\n";
  }
else{
  echo "Match not found"."\n";
  }
?>

Output:

Match not found
Match found
Match found

The main difference between in_array and array_search is that in_array function only returns TRUE or FALSE based on the result of searching if they exist in an array. While the array_search() function usually returns a value, either a key or an index using a conditional statement.

How to use PHP in_array with associative array?

To use PHP in_array with an associated array, we can use the following code as a function to get the associated array. Just always keep in mind that the key value is case-sensitive manner and cannot be accessed anywhere on the entire array of elements.

Example:

<?php 
function is_in_array($array, $key, $key_value){
      $within_array = 'no';
      foreach( $array as $k=>$v ){
        if( is_array($v) ){
            $within_array = is_in_array($v, $key, $key_value);
            if( $within_array == 'yes' ){
                break;
            }
        } else {
                if( $v == $key_value && $k == $key ){
                        $within_array = 'yes';
                        break;
                }
        }
      }
      return $within_array;
}
$test = array(
                0=> array('ID'=>1, 'programmerName'=>"Glenn"), 
                1=> array('ID'=>2, 'ProgrammerName'=>"Jude")
        );
print_r(is_in_array($test, 'programmerName', 'Glenn'));
?>

Output:

yes

Using in_array for multi dimensional arrays

The in_array In PHP can only be called on a single dimensional array. In order to be used, we need to loop over each (sub array) and execute in_array on each element.

Further, based on what I observed, the function is only for a (2 dimensional array). If there will be instances that you have more nested arrays, I will recommend a recursive version on that matter to check if a value exists.

Example:

<?php
$array = array(
   "0" => array(
      "programmerName"  => "Jude",
      "0"               => "Jude"
   ),
   "1" => array(
      "programmerName"  => "Glenn",
      "0"               => "Glenn"
   ),
   "2" => array(
      "programmerName"  => "Adones",
      "0"               => "Adnoes"
   )
);

$searchprogrammer = "Glenn";

foreach($array as $v) {
   if ($v['programmerName'] == $searchprogrammer) {
      $found = true;
   }
}

echo (isset($found) ? 'Programmer name found!' : 'search failed');
?>

Output:

Programmer name found!

in_array for a combo value (‘test’,’value’)

if (
    array_key_exists('test', $array) && $array['test'] == 'value' // Has test => value
    ||
    in_array(array('test', 'value'), $array) // Has [test, value] and strict mode data types
) {
    // Found
}

Php in_array find value by key

in_array($id, array_column($array, 'id'))

More About Array

Summary

In summary, you have learned about PHP in_array. This article also discussed what is in_array PHP, what is the difference between in_array and Array_search, how to use PHP in_array with an associative array, using in_array for multidimensional arrays, in_array for a combo value (‘test’, ‘value’) and PHP in_array find value by key.

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.

Leave a Comment