PHP Function search_multidimensional_array() to Search Multidimensional Array

search_multidimensional_array() works with unlimited multidimensional arrays both associative arrays and indexed arrays and the function can be set to ignore case if desired.

Home Short:

array_search() is a great php function for finding if a value is an array but it only works with a one dimensional array.

The custom function below search_multidimensional_array() works with unlimited multidimensional arrays both associative arrays and indexed arrays and the function can be set to ignore case if desired.

As an example lets assume the following multidimensional array ...

$marks = array(
    
    "Ankit" => array(
        
        "C" => array(

             "xx" => 178,
             "ss" => 198,
             "aa" => array(
                  "necro" => "helper",
                  "monkey" => "oo oo",
                  "dog" => "pitbull"
               ),
             ),
             
        "DCO" => 85,
        "FOL" => 74,
    ),
        
    "Ram" => array(

        "C" => 78,
        "DCO" => 98,
        "FOL" => 46,
    ),

    "Anoop" => array(

        "C" => 88,
        "DCO" => 46,
        "FOL" => 99,
    ),
);

search_multidimensional_array("helper", $marks); ...

returns Array ( [Ankit] => Array ( [C] => Array ( [aa] => necro ) ) )

An example of using the function search_multidimensional_array() with an indexed array ...

$students = array(
    array("Alice", 22, "steve"),
    array("Bob", 20, array("Science", array("A+", "F"))),
    array("Charlie", 23, array("Literature", "History")),
);

search_multidimensional_array("A+", $students); ...

returns Array ( [1] => Array ( [2] => Array ( [1] => 0 ) ) )

S
H
A
R
E