Search Multidimensional Array for Key Value in PHP

On 12/23/2015

Arrays are generally used to store and manipulate data with ease and known to be one of the core building blocks of PHP Programming. When properly used, arrays are a great tool to process large amount of data efficiently - be it from database server or from a third-party source. But they can become nasty once you start working with multidimensional array which by itself is an array of arrays i.e., every element of the array turns out as an array. Below example is a good representation of a multi-dimensional array.

$color = Array (
    ('r') => Array (
        ('red') => 255,
        ('green') => 0,
        ('blue') => 0
    ),
    ('g') => Array (
        ('red') => 0,
        ('green') => 255,
        ('blue') => 0
    ),
    ('b') => Array (
        ('red') => 0,
        ('green') => 0,
        ('blue') => 255
    )
);

Searching through this sort of multidimensional array for some key or value will be a challenge you should face as a php beginner. Here we’ll see how to search multidimensional array for key and value efficiently in php.

PHP Search Array for Key

If you want to retrieve the value of a specific key, then it’s pretty straightforward and can be accessed like this,

<?php
print_r($color['r']);

// output
// Array ( [red] => 255 [green] => 0 [blue] => 0 )
?>

But searching through the nested array values needs some more effort and here I have given two different ways to handle it.

Method 1: Search Multidimensional Array for Key Value

This method applies to PHP versions <= 5.4 since there are no built-in functions for performing this sort of array search. We have to do it manually and here is the PHP function to search for the nested array values and return the key from the multidimensional array.

<?php
// php function to search multi-dimensional array
function searchArray($key, $st, $array) {
   foreach ($array as $k => $v) {
       if ($v[$key] === $st) {
           return $k;
       }
   }
   return null;
}

// define multidimensional array
$color = Array (
    ('r') => Array (
        ('red') => 255,
        ('green') => 0,
        ('blue') => 0
    ),
    ('g') => Array (
        ('red') => 0,
        ('green') => 255,
        ('blue') => 0
    ),
    ('b') => Array (
        ('red') => 0,
        ('green') => 0,
        ('blue') => 255
    )
);

// search for key
echo "The Key is " . searchArray('blue', 255, $color);

// output
// The Key is b
?>

Method 2: Applies for PHP V5.5 and Above

As for PHP versions 5.5+ the process of searching multidimensional array is a one-liner code.

<?php
echo "The Key is " . array_search(255, array_column($color, 'blue'));

// output
// The Key is b
?>
  • The function array_search() searches the given array for a specific value and returns it’s key.
  • The function array_column() returns the values of the specified column from the array.

That explains about searching multidimensional array for key and value in php language.

1 comment:

  1. Thanks for splitting your comprehension with us. PHP- It’s an open source server-side scripting language particularly designed for web development but even it’s used as a universal programming language. Make our website as user friendly and responsive, it helps you to get more visibility &familiarity in online.
    Regards,
    PHP Training in Chennai|PHP Course in Chennai|PHP Training

    ReplyDelete

Contact Form

Name

Email *

Message *