Skip to main content
PHP Blog

Back to all posts

How to Search A Multi-Level Array In PHP?

Published on
5 min read
How to Search A Multi-Level Array In PHP? image

Best PHP Multi-Level Array Search Techniques to Buy in October 2025

1 PHP & MySQL: Server-side Web Development

PHP & MySQL: Server-side Web Development

BUY & SAVE
$29.21 $45.00
Save 35%
PHP & MySQL: Server-side Web Development
2 Programming PHP: Creating Dynamic Web Pages

Programming PHP: Creating Dynamic Web Pages

BUY & SAVE
$36.49 $65.99
Save 45%
Programming PHP: Creating Dynamic Web Pages
3 PHP Crash Course: The Complete, Modern, Hands-On Guide

PHP Crash Course: The Complete, Modern, Hands-On Guide

BUY & SAVE
$53.53 $69.99
Save 24%
PHP Crash Course: The Complete, Modern, Hands-On Guide
4 Front-End Back-End Development with HTML, CSS, JavaScript, jQuery, PHP, and MySQL

Front-End Back-End Development with HTML, CSS, JavaScript, jQuery, PHP, and MySQL

BUY & SAVE
$66.29 $95.00
Save 30%
Front-End Back-End Development with HTML, CSS, JavaScript, jQuery, PHP, and MySQL
5 Full Stack Web Development For Beginners: Learn Ecommerce Web Development Using HTML5, CSS3, Bootstrap, JavaScript, MySQL, and PHP

Full Stack Web Development For Beginners: Learn Ecommerce Web Development Using HTML5, CSS3, Bootstrap, JavaScript, MySQL, and PHP

BUY & SAVE
$35.00
Full Stack Web Development For Beginners: Learn Ecommerce Web Development Using HTML5, CSS3, Bootstrap, JavaScript, MySQL, and PHP
6 Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

BUY & SAVE
$39.03 $65.99
Save 41%
Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)
7 Murach's PHP and MySQL (4th Edition)

Murach's PHP and MySQL (4th Edition)

BUY & SAVE
$42.38
Murach's PHP and MySQL (4th Edition)
+
ONE MORE?

To search a multi-level array in PHP, you can use recursive functions to traverse through each level of the array until you find the desired value.

Here is an example of a recursive function that searches for a specific value in a multi-level array:

function searchValue($array, $searchValue) { foreach($array as $key => $value) { if ($value === $searchValue) { return true; // Value found } elseif (is_array($value)) { if (searchValue($value, $searchValue)) { return true; // Value found in nested array } } } return false; // Value not found }

// Example usage $nestedArray = array( "level1" => array( "level2" => array( "level3" => "searchedValue" ) ), "otherKey" => "otherValue" );

$searchValue = "searchedValue"; if (searchValue($nestedArray, $searchValue)) { echo "Value found!"; } else { echo "Value not found!"; }

In this example, the searchValue function takes two parameters: the array to search through and the value to search for. It iterates through each element of the array and checks if the current value matches the search value. If they match, the function returns true. If the value is an array, the function calls itself recursively to search within that nested array.

By using recursive functions like this, you can effectively search for values within multi-level arrays in PHP.

What is the function to flatten a multi-level array in PHP?

In PHP, you can use the array_merge function to flatten a multi-level array. Here is an example:

function flattenArray($array) { $result = [];

foreach ($array as $item) {
    if (is\_array($item)) {
        $result = array\_merge($result, flattenArray($item));
    } else {
        $result\[\] = $item;
    }
}

return $result;

}

// Example usage $multiLevelArray = [1, [2, 3], [4, [5, 6]], 7]; $flattenedArray = flattenArray($multiLevelArray); print_r($flattenedArray);

Output:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 )

What is the syntax for accessing elements in a multi-level array in PHP?

To access elements in a multi-level array in PHP, you can use square brackets [] and multiple levels of array keys. Here is the syntax:

$arrayName['level1']['level2']['level3'];

Here's an example:

$students = array( 'John' => array( 'age' => 21, 'grade' => 'A' ), 'Jane' => array( 'age' => 19, 'grade' => 'B' ) );

echo $students['John']['age']; // Output: 21 echo $students['Jane']['grade']; // Output: B

In the example above, we have a multi-level array named $students. To access elements within it, we use $students['John']['age'] to access John's age, and $students['Jane']['grade'] to access Jane's grade.

How to search for partial matches within a multi-level array in PHP?

To search for partial matches within a multi-level array in PHP, you can use recursive functions. Here's an example of how you can achieve this:

function searchPartialMatches($array, $keyword) { $results = [];

foreach ($array as $key => $value) {
    if (is\_array($value)) {
        $subResults = searchPartialMatches($value, $keyword);
        
        if (!empty($subResults)) {
            $results\[$key\] = $subResults;
        }
    } else {
        if (stripos($value, $keyword) !== false) {
            $results\[$key\] = $value;
        }
    }
}

return $results;

}

// Example usage: $data = [ 'fruits' => [ 'apple', 'banana', 'grape', ], 'animals' => [ 'lion', 'tiger', 'elephant', ], 'colors' => [ 'red', 'blue', 'green', ], ];

$keyword = 'an'; $results = searchPartialMatches($data, $keyword);

print_r($results);

In this example, we have a multi-level array named $data. We want to search for partial matches of the keyword "an" within this array.

The searchPartialMatches() function is a recursive function that iterates through each element of the array. If an element is also an array, it recursively calls itself with that element until we reach a non-array value. If the current value is a non-array value, it checks whether the keyword is present using the stripos() function (which performs a case-insensitive search). If a match is found, it adds the key-value pair to the $results array.

After calling the searchPartialMatches() function with the $data array and the desired $keyword, we print the results using print_r().

The output of the example usage will be:

Array ( [fruits] => Array ( [0] => banana [2] => grape )

\[animals\] => Array
    (
        \[1\] => tiger
        \[2\] => elephant
    )

)

This output shows the partial matches for the keyword "an" within the multi-level array.

How to delete an element from a multi-level array in PHP?

To delete an element from a multi-level array in PHP, you can use a combination of recursive functions and array manipulation techniques. Here's an example of how you can achieve this:

function deleteElement(&$array, $value) { foreach ($array as $key => &$element) { if ($element === $value) { unset($array[$key]); // remove the element } elseif (is_array($element)) { deleteElement($element, $value); // recursively delete element in sub-arrays } } }

// Example usage: $multiLevelArray = [ 'A' => [ 'B' => 'Value 1', 'C' => [ 'D' => 'Value 2', 'E' => 'Value 3', ], 'F' => 'Value 4', ], ];

deleteElement($multiLevelArray, 'Value 2'); print_r($multiLevelArray);

In this example, we define a recursive function deleteElement that accepts the multi-level array and the value you want to delete as parameters. It iterates over each element of the array and checks if the element matches the given value. If it does, it uses the unset function to remove the element from the array. If the element is an array itself, it recursively calls the deleteElement function to remove the value from the sub-array.

In the given example, the value 'Value 2' is deleted from the $multiLevelArray and the updated array is printed using print_r.