PHP Multidimensional Array Search by Key and Value with Examples

PHP Multidimensional Array Search by Key and Value with Examples

In this article, we would love to shows, how you can create your own function for searching PHP Multidimensional Array. Here we will take two examples for searching in the multidimensional array using custom created function.

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 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 custom created function.

PHP Search Multidimensional Array

You have one array look like this:

$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

Fastest way to search a multidimensional array. In this tutorial, you have learned how to search in a multidimensional array by key and value.

php web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...

Hire Web Developer

Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...