0

I want to unset array elements from my nested array of arrays.

I want to perform recursive array filter and i am not sure about how to do that efficiently.

Below is the original array.

[
  [a] => 3
  [b] => 0
  [c] => [
    [
      [1] => aa
      [2] => 1
      [3] => [
                [
                  [a1] => 6
                  [a2] => 5781
                  [a3] =>
                      [
                        [1] => 0
                        [2] => 19550
                        [3] => 5781
                      ]
                ]
                [
                  [a1] => 1
                  [a2] => 5781
                  [a3] =>[
                          [1] => 0
                          [2] => 19550
                          [3] => 5781
                      ]
                ]
              ]
    ]
    [
      [1] => aa
      [2] => 1
      [3] => [
                [
                  [a1] => 6
                  [a2] => 5781
                  [a3] =>
                      [
                        [1] => 0
                        [2] => 19550
                        [3] => 5781
                      ]
                ]
                [
                  [a1] => 1
                  [a2] => 5781
                  [a3] =>[
                          [1] => 0
                          [2] => 19550
                          [3] => 5781
                      ]
                ]
              ]
    ]
  ]
]

Expected array

[
  [a] => 3
  [c] => [
    [
      [1] => aa
      [3] => [
                [
                  [a1] => 6
                  [a3] =>
                      [
                        [2] => 19550
                        [3] => 5781
                      ]
                ]
                [
                  [a1] => 1
                  [a3] =>[
                          [2] => 19550
                          [3] => 5781
                      ]
                ]
              ]
    ]
    [
      [1] => aa
      [3] => [
                [
                  [a1] => 6
                  [a3] =>
                      [
                        [2] => 19550
                        [3] => 5781
                      ]
                ]
                [
                  [a1] => 1
                  [a3] =>[
                          [2] => 19550
                          [3] => 5781
                      ]
                ]
              ]
    ]
  ]
]

As you can see in my expected array from every nested arrays some key value pairs have been removed. I need to filter the array based on key name. That is my key names are fixed and that needs to be removed from every children arrays inside.

Any helps would be appreciated.

8
  • Possible duplicate of How to run array_filter recursively in a PHP array? Commented Mar 6, 2019 at 13:49
  • 1
    Curious, why don't you want to use a foreach? Commented Mar 6, 2019 at 13:49
  • Already checked that link but i am not clear with that as i need it over array inside array and also want over the innermost array.@Script47 Commented Mar 6, 2019 at 13:53
  • So should i use nested foreach's???? Commented Mar 6, 2019 at 13:54
  • 1
    or a recursive function? Commented Mar 6, 2019 at 13:54

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Your ans helped a lot.Just a update about the bug you already told that in_array function matches 0 also even though it is not present.So this can be fixed by using in_array in strict mode as (in_array($key, $keys,true)). Hope it helps someone.
Nice! Great catch on the "strict" parameter. Glad I could help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.