简体   繁体   中英

Php Array_filter to unset key values from array of arrays

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM