0

So I have the following array:

$array = array(array('fruit1' => 'apple'), 
               array('fruit2' => 'orange'), 
               array('veg1' => 'tomato'), 
               array('veg2' => 'carrot'));

and I want to run a function like this:

array_remove_recursive($array, 'tomato');

so the output is this:

$array = array(array('fruit1' => 'apple'), 
               array('fruit2' => 'orange'), 
               array('veg2' => 'carrot')); // note: the entire array containing tomato has been removed!

Is this solvable?

Jonas Kaufmann
  • 1,797
  • 3
  • 22
  • 43
  • @cmorrissey http://stackoverflow.com/questions/1708860/php-recursively-unset-array-keys-if-match http://stackoverflow.com/questions/12053433/recursive-search-and-remove-in-array http://stackoverflow.com/questions/7696548/php-how-to-remove-empty-entries-of-an-array-recursively – Jonas Kaufmann Jun 26 '14 at 19:30

3 Answers3

0

This will recursively unset the matching variable at any depth and then remove the parent element only if it is empty.

function recursive_unset(array &$array, $unwanted_val) {
    foreach ($array as $key => &$value) {
        if (is_array($value)) {
            recursive_unset($value, $unwanted_val);
            if(!count($value)){
                unset($array[$key]);
            }
        } else if($value == $unwanted_val){
            unset($array[$key]);
        }
    }
}
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
0

Function to remove recursively many values from multidimensional array.

# Example 1
$arr1 = array_remove_recursive($arr, 'tomato');

# Example 2
$arr2 = array_remove_recursive($arr, ['tomato', 'apple']);

function array_remove_recursive($arr, $values)
{
    if (!is_array($values))
        $values = [$values];

    foreach ($arr as $k => $v) {
        if (is_array($v)) {
            if ($arr2 = array_remove_recursive($v, $values))
                $arr[$k] = $arr2;
            else
                unset($arr[$k]);
        } elseif (in_array($v, $values, true))
            unset($arr[$k]);
    }
    return $arr;
}
Vlad Alivanov
  • 1,134
  • 9
  • 9
-1
function array_remove_recursive($getArray,$getAssocValue)
{
    $returnArray    = array();
    if(is_array($getArray))
    {
        foreach($getArray as $indAssocValue)
        {
            if(is_array($indAssocValue))
            {
                foreach($indAssocValue as $innerKey=>$innerVal)
                {
                    if($innerVal!=$getAssocValue and $innerKey!="")
                    {
                        $returnArray[]  = array("$innerKey"=>$innerVal);
                    }
                }
            }
        }
    }
    return $returnArray;
}

$array = array(array('fruit1' => 'apple'), 
               array('fruit2' => 'orange'), 
               array('veg1' => 'tomato'), 
               array('veg2' => 'carrot'));
               print_r($array);
               echo "<br />";
$array  = array_remove_recursive($array, 'tomato');
print_r($array);

hope the above code would be helpfull.

ameenulla0007
  • 2,663
  • 1
  • 12
  • 15