PHP Recursively Unset Array Keys If Match

PHP Recursively unset array keys if match

If you want to operate recursively, you need to pass the array as a reference, otherwise you do a lot of unnecessarily copying:

function recursive_unset(&$array, $unwanted_key) {
unset($array[$unwanted_key]);
foreach ($array as &$value) {
if (is_array($value)) {
recursive_unset($value, $unwanted_key);
}
}
}

Class method to recursively remove keys from array

You shold pass all the $keys rather than just one $key when calling your function recursively here:

$this->remove($value, $key);

Also note that the $key in this context comes from the original array, not the one with all the keys to be removed.

Recursive search and remove in array?

You can ease things by only using references.

function removeKey($key, &$array, $childKey = 'children'){
if(isset($array[$key])){
unset($array[$key]);
return;
}

foreach($array as &$item)
if(isset($item[$childKey]))
removeKey($key, $item[$childKey], $childKey);
}

Example:

$arr = array(...);
removeKey('key', $arr, $chilKey);
// Just continue using $arr

Php Array_filter to unset key values from array of arrays

Here is an example of a recursive foreach based solution to your code working off the data set you provided.

    $sourceArray = array("a" => 3, "b" => 0, "c" => array("1" => "aa", "2" => 1, "3" => array("a1" => 6, "a2" => 5781, "a3" => array("1" => 0, "2" => 19550, "3" => 5781)), array( "a1" => 1, "a2" => 5781, "a3" =>array("1" => 0, "2" => 19550, "3" => 5781 ))), array( "1" => "aa", "2" => 1, "3" => array( array( "a1" => 6, "a2" => 5781, "a3" => array( "1" => 0, "2" => 19550,"3" => 5781))), array( "a1" => 1, "a2" => 5781, "a3" =>array(  "1" => 0, "2" => 19550, "3" => 5781))));

print_r($sourceArray,1);

function removeKeys($keys, $sourceData) {

foreach ($sourceData as $key=>$val) {

if (in_array($key, $keys, true)) {
unset($sourceData[$key]);
} else if (is_array($val)) {
$sourceData[$key] = removeKeys($keys, $sourceData[$key]);
}
}
return $sourceData;
}

$keysToRemove = array("b","2","a2");

$newArray = removeKeys($keysToRemove, $sourceArray);

print_r($newArray);

Simple to implement though getting your data in was a bit of a challenge. I did notice a "bug" in this example in that if the key is "0" in the original array it still gets deleted even if it's not in the $keys array.

But I'm assuming that this example is sufficient to answer your question and that my stated edge case will not occur (ie, "0" is not a key value in your array.) If you do use "0" as a key you can add additional logic to trap that case but it would slow the function down a bit so I'll leave that choice up to you.

(Note, the bug referred to above is fixed now and in the code... see notes below for solution from original poster)

Recursively remove element by value

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.



Related Topics



Leave a reply



Submit