PHP search a multidimensional array (Search By key and Value). Here we will learn how to search in the multidimensional array for value and return key.
Sometimes we need to search in an array or multidimensional array by key or value without using any function. This tutorial shows you, the fastest way to search in a multidimensional array.
In this article, we would love to shows, how you can create your own function for searching Multidimensional Array. Here we will take two examples for searching in the multidimensional array using the custom created function.
Sometimes we need to integrate a multidimensional array search by value in php. Here you will learn php multidimensional array search by value. We are going to show you about php multidimensional array search key by value. We will look at example of how to search value in multidimensional array in php.
What are Multidimensional Arrays
An array containing one or more array called a multidimensional array. PHP supports to unlimited level deep. You can create as much as a required array inside the array in the PHP and use it in the application as per need. However, more than 3 level becomes hard to manage.
PHP search a multidimensional array for value and return key
If you want to search in multidimensional-array by value and return key. So you can use this below example for that:
<?php $array = array( array( 'id' => '100', 'name' => 'Rock', ), array( 'id' => '105', 'name' => 'Test', ), array( 'id' => '109', 'name' => 'Michael', ), array( 'id' => '111', 'name' => 'Mack', ) ); function searchByValue($id, $array) { foreach ($array as $key => $val) { if ($val['id'] === $id) { $resultSet['name'] = $val['name']; $resultSet['key'] = $key; $resultSet['id'] = $val['id']; return $resultSet; } } return null; } $searchValue = searchByValue('105', $array); print_r($searchValue);die; ?>
Output
Array
(
[name] => Rangle
[key] => 1
[id] => 105
)
PHP search a multidimensional array for key and return value
If you want to search in a multidimensional array by key and return value. So you can use the below example for that:
<?php $array = array( array( 'id' => '100', 'name' => 'Rock', ), array( 'id' => '105', 'name' => 'Test', ), array( 'id' => '109', 'name' => 'Michael', ), array( 'id' => '111', 'name' => 'Mack', ) ); function searchByKey($keyVal, $array) { foreach ($array as $key => $val) { if ($keyVal == $key) { $resultSet['name'] = $val['name']; $resultSet['key'] = $key; $resultSet['id'] = $val['id']; return $resultSet; } } return null; } $searchByKey = searchByKey('2', $array); print_r($searchByKey);die; ?>
Output
Array
(
[name] => Michael
[key] => 2
[id] => 109
)
Note:
It is important to know that if you are using === operator compared types have to be exactly same, in this example you have to search string or just use == instead ===.
Conclusion
The fastest way to search a multidimensional array. In this tutorial, you have learned how to search in a multidimensional array by key and value. Hope this tutorial helped you. If you have any queries, please mentioned that in the below comment.
You can also check How to Remove Null Values From Multidimensional Array In PHP