PHP Search Multidimensional Array By key, value and return key


php search multidimensional array by key and value. Through this tutorial, we will learn how to search in the multidimensional array for value and return key. And also learn how to search multidimensional array for key and return value.

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 show you, how you can create your own function for searching Multidimensional Array. Here we will take two examples for searching in the multidimensional array using a custom created function.

PHP Search Multidimensional Array By key, value and return key

  • PHP search multidimensional array for value and return key
  • PHP search multidimensional array for key and return value

Let, us have one array look like this and want to search by key and value and return key:

    $array = array(
        array(
            'id' => '100',
            'name' => 'Rock',
        ),
        array(
            'id' => '105',
            'name' => 'Test',
        ),
        array(
            'id' => '109',
            'name' => 'Michael',
        ),
        array(
            'id' => '111',
            'name' => 'Mack',
        )
    );

PHP search 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;

 ?>
    

PHP search 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;

 ?>
    

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, we have learned how to search in a multidimensional array by key and value.

Recommended PHP Tutorials

  1. PHP Array: Indexed,Associative, Multidimensional
  2. To Remove Elements or Values from Array PHP
  3. PHP remove duplicates from multidimensional array
  4. Remove Duplicate Elements or Values from Array PHP
  5. How to Convert String to Array in PHP
  6. Array Push and POP in PHP | PHP Tutorial
  7. PHP Array to String Conversion – PHP Implode
Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
Jassweb, Rai Chak, Punjab, India. 143518