Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / PHP

Recursively searching and removing a value from a multidimensional array in PHP

Rate me:
Please Sign up or sign in to vote.
4.45/5 (4 votes)
24 Dec 2011CPOL1 min read 37.5K   173   8   3
Search and remove a value from a PHP array/multidimensional array recursively.

Introduction

Recently I found myself in a situation where I needed to search and remove a value from a dynamically generated multidimensional array with a different size and length. After many trials and errors, I found that the best solution is to recursively loop through the array and locate and remove the value.

Using the code

Assuming we want to remove all the elements containing "FirstValue" from an array, for example:

PHP
$value = "FirstValue"; //value/element we want to remove

And we have the following multidimensional array:

PHP
$array["First"] = "FirstValue";
$array["Second"] = "SecondValue";
$array["Third"]["First"] = "FirstValue";
$array["Third"]["Second"] = "SecondValue";
$array["Fourth"]["Third"]["First"] = "FirstValue";
$array["Fourth"]["Third"]["Second"] = "SecondValue";
$array["Fifth"]["Fourth"]["Third"]["First"] = "FirstValue";
$array["Fifth"]["Fourth"]["Third"]["Second"] = "SecondValue";

Using a simple print_r($array) on the above array, will print the following:

Array
(
    [First] => FirstValue
    [Second] => SecondValue
    [Third] => Array
        (
            [First] => FirstValue
            [Second] => SecondValue
        )

    [Fourth] => Array
        (
            [Third] => Array
                (
                    [First] => FirstValue
                    [Second] => SecondValue
                )

        )

    [Fifth] => Array
        (
            [Fourth] => Array
                (
                    [Third] => Array
                        (
                            [First] => FirstValue
                            [Second] => SecondValue
                        )

                )

        )
)

In order to remove all the elements containing the value "FirstValue" from this multidimensional array, we call the recursiveRemoval($array, $value) function as follows:

PHP
recursiveRemoval($array, $value);

The recursiveRemoval function code is as follows:

PHP
function recursiveRemoval(&$array, $val)
{
    if(is_array($array))
    {
        foreach($array as $key=>&$arrayElement)
        {
            if(is_array($arrayElement))
            {
                recursiveRemoval($arrayElement, $val);
            }
            else
            {
                if($arrayElement == $val)
                {
                    unset($array[$key]);
                }
            }
        }
    }
}

First thing we're doing is checking if the array passed to the function is actually an array. If array passed is an array, we iterate through its elements. If the element is also an array (our array is not a one dimensional array), we recursively call the function recursiveRemoval($arrayElement, $val) again to break the multidimensional array into one dimensional arrays.

At the end, we process the one dimensional array elements, and we compare them to the passed value we want to remove, and we unset the key of the array ($array[$key]).

Conclusion

I wrote this piece of code during work, to search and remove elements from a dynamically generated multidimensional array. Recursion can be a life saver in many cases, hopefully my code will help you if you ever find yourself in the same situation as mine.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey23-Feb-12 19:37
professionalManoj Kumar Choubey23-Feb-12 19:37 
GeneralRe: My vote of 5 Pin
Etienne Rached15-Mar-12 13:57
Etienne Rached15-Mar-12 13:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.