PHP in_array function use and examples


Home - Tutorials - Basics

This short article how to use PHP in_array function with arrays. See the description with usage details and reusable code examples below. The details of how to use in_array with normal or multidimensional arrays is also included.

Tutorial info:


Name:PHP in_array function use and examples
Total steps:1
Category:Basics
Level:Beginner

Bookmark PHP in_array function use and examples



Bookmark and Share

Step 1 - PHP in_array function usage examples


PHP in_array function use and examples

 

In cases when you want to check whether a value exists in an array you can use the in_array function. This function checks if a value exists in an array. You can use it from PHP version 4. The syntax is the following:

bool in_array ( mixed $what , array $where [, bool $strict = FALSE ] )

By default you simply use only the 2 mandatory parameters ($what and $where) as you can see in the following example:

Code:
  1. $myArray = array("Audi","BMW","Lexus","Mercedes");
  2.  
  3. var_dump($myArray);
  4.  
  5. if (in_array("Lexus", $myArray)) {
  6.     echo "Lexus was found in the array<br/>";
  7. }
  8.  
  9. if (in_array("opel", $myArray)) {
  10.     echo "Opel was found in the array<br/>";
  11. }
  12.  

The strict parameter:

If you want to force th in_array function to check also variable types then, you need to set the optional strict parameter to true as you can see in the next code example:

Code:
  1. $myArray = array(10,20,30,40,50);
  2.  
  3. var_dump($myArray);
  4.  
  5. if (in_array("10", $myArray)) {
  6.     echo "A - 10 was found as string in the array without strict<br/>";
  7. }
  8.  
  9. if (in_array("10", $myArray, TRUE)) {
  10.     echo "B - 10 was found as string in the array using strict<br/>";
  11. }

Using in_array to find key in an associative array. 

The in_array function only checks the values in the array and so if you want to check the keys in case of an associative array then you need to use the array_key_exists function as demonstrated below:

Code:
  1. $myArray = array("Audi"=>"A4","Audi"=>"A6","Lexus"=>"IS","Lexus"=>"LS");
  2.  
  3. var_dump($myArray);
  4.  
  5. if (in_array("Lexus", $myArray)) {
  6.     echo "Lexus was found in the array<br/>"; 
  7. }
  8.  
  9. if (array_key_exists("Lexus", $myArray)) {
  10.     echo "Lexus key was found in the array<br/>";
  11. }
  12.  

Using in_array to find values in a multidimensional array. 

The next general question is how to find a value in a multidimensional array. Unfortunately in_array is not able to handle multidimensional arrays so you need to create your own function to solve this problem. However with a simple recursive function you can do this as you can see in the next example code:

Code:
  1. function in_array_multi($needle, $haystack) {
  2.     foreach ($haystack as $item) {
  3.         if ($item === $needle || (is_array($item) && in_array_multi($needle, $item))) {
  4.             return true;
  5.         }
  6.     }
  7.  
  8.     return false;
  9. }
  10.  
  11. $myArray = array(array(10,20),array(11,22),array(111,222));
  12.  
  13. var_dump($myArray);
  14.  
  15. if (in_array(11, $myArray)) {
  16.     echo "11 was found";
  17. }
  18.  
  19. if (in_array_multi(11, $myArray)) {
  20.     echo "11 was found with multi";
  21. }

PHP in_array function 







Tags: php in_array function, in_array use, in_array example, in_array multidimensional, in_array key, in_array associative

PHP in_array function use and examples - Table of contents
Step 1 - PHP in_array function usage examples


Follow phpf1 on Twitter




F1 Site Family
AJAX F1
HTML F1
Java F1
JavaScript F1
PHP F1

Total time: 0.0603