PHP Tutorials

PHP 8 Search in Arrays Tutorial with Examples

In this tutorial, we will look at how to search in arrays using PHP 8’s pre-built functions. PHP offers a wide range of built-in functions to deal with a different kind of situation in the programming world.

Working with arrays in php is made simple by its some standard built-in functions like array_search, array_key_exists, keys, and in_array. We’ll find out how to search through the arrays in PHP below:

Search in Array using PHP array_search function

Inject the value as a first parameter in the array_search() function, and this php function search the passed value’s key in an array (2nd parameter). The second parameter is the array itself, which has to be searched. Then it will return the matched key from the array collection. However, It will return false if no value is found. Have a look in the example below:

$myarray = ['toy story 4', 'black panther', 'captain marvel', 'spider man'];

// find 'black panther' in $myarray
var_dump( array_search('black panther', $myarray) ); 

// output: int(1)

As you can see in the above example the PHP array_search function returned int(1) because a match was found in the array.

The Strict Parameter Example:

This is an optional parameter and mainly used for identical search in an array, the default value is false.

Find out what happens when strict value is set to false:

$myarray = [15, 19, 33, 10, 19, 13, 20];
$value = 19;
var_dump( array_search($value, $myarray) ); 
// output : 1

Let’s check out how will array_search function behaves when the strict argument is set to true:

$myarray = [15, 19, 33, 10, 19, 13, 20];
$value = "19";
var_dump( array_search($value, $myarray, true) ); 

// output : bool(false)

Reference: www.php.net

Get Array Keys using PHP array_keys function

Getting array keys can be done using the array_keys() function in PHP. This method takes arrays as an argument and returns almost every key of the array. However, if you supply search value as a second parameter in the array_keys function, then it will return the key if found in the array.

Get Single Key from Array with Value Passed Example

$car_colors = [
    'bmw' => 'royal blue',
    'ford' => 'black',
    'volvo' => 'blue',
    'suzuki' => 'red'
];

print_r( array_keys($car_colors, 'red') );

// output: Array ( [0] => suzuki )

Get All Keys from PHP Array

$car_colors = [
    'bmw' => 'royal blue',
    'ford' => 'black',
    'volvo' => 'blue',
    'suzuki' => 'red'
];

print_r( array_keys($car_colors) );


/* output: 
  Array ( 
    [0] => bmw 
    [1] => ford 
    [2] => volvo 
    [3] => suzuki 
  )
*/

Get Value in Array using PHP in_array Function

The in_array function in php is used to get the specific value in the array. It returns either true or false. It’s a very helpful function to find whether the value exists in the array or not.

This method takes 3 parameters. In 1st parameter pass the value which needs to be searched. The 2nd parameter takes an array.

$movies = ['toy story 4', 'black panther', 'captain marvel', 'spider man'];

if (in_array("black panther", $movies))
 {
  echo "Found the value";
 }
else
 {
  echo "Value doesn't exist";
 }

/* output: Found the value */

The `type` is supplied to the in_array function as the 3rd optional parameter, if this parameter is true, it searches for the search-string in the array.

Get Key in Arrays using PHP array_key_exists Function

The array_key_exists() function is used to check whether an array for a particular key exists in the array or not. It the key exists. It returns true. If the value is non-existing will return to false.

$car_colors = [
    'bmw' => 'royal blue',
    'ford' => 'black',
    'volvo' => 'blue',
    'suzuki' => 'red'
];

if (array_key_exists("volvo", $car_colors))
  {
  echo "Key exists in array!";
  }
else
  {
  echo "Key doesn't exist in array!";
  }


/* output: Key exists in array! */

See, what happens when value doesn’t exist in array:

$car_colors = [
    'bmw' => 'royal blue',
    'ford' => 'black',
    'volvo' => 'blue',
    'suzuki' => 'red'
];

if (array_key_exists("lamborghini", $car_colors))
  {
  echo "Key exists in array!";
  }
else
  {
  echo "Key doesn't exist in array!";
  }


/* output: Key doesn't exist in array! */

Conclusion

Finally we have finished this PHP 8 Search in Arrays example tutorial, in this tutorial we learnt the various methods to search an item in an array. We got to know about popularly used built-in PHP functions like: array_search, array_key_exists, array_keys and in_array. Please share this tutorial with others, if you find this tutorial helpful. Thanks for reading, have a good day!

Digamber

I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.

Recent Posts

React Router DOM 6 Fetch Current URL / Pathname Tutorial

If you want to learn how to get the current route's name, component pathname or…

2 days ago

React Js Hide & Show with React Bootstrap Collapse Tutorial

React show and hide example. In this tutorial, we will show you how to step…

5 days ago

How to Integrate Tabs in React Js with React Bootstrap

Tabs are one of the best UI elements for displaying multiple contents in a single…

1 week ago

How to Build Toast Component in React Js with Bootstrap

In this tutorial, we will learn how to create a toast notification component in the…

2 weeks ago

React Js Bootstrap Modal Popup Component Tutorial

Bootstrap offers tons of custom UI solutions. On top of that, it is easy to…

2 weeks ago

React Build Counter using useReducer Hook Tutorial

React js counter using the useReducer hook example. In this post, we will learn how…

2 weeks ago