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:
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.
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
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.
$car_colors = [
'bmw' => 'royal blue',
'ford' => 'black',
'volvo' => 'blue',
'suzuki' => 'red'
];
print_r( array_keys($car_colors, 'red') );
// output: Array ( [0] => suzuki )
$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
)
*/
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.
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! */
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!
If you want to learn how to get the current route's name, component pathname or…
React show and hide example. In this tutorial, we will show you how to step…
Tabs are one of the best UI elements for displaying multiple contents in a single…
In this tutorial, we will learn how to create a toast notification component in the…
Bootstrap offers tons of custom UI solutions. On top of that, it is easy to…
React js counter using the useReducer hook example. In this post, we will learn how…