PHP Array_filter取消设置数组中的键值

时间:2019-03-06 13:46:21

标签: php laravel-5

我要从嵌套的数组数组中取消设置数组元素。

我想执行递归数组过滤器,但我不确定如何有效地做到这一点。

下面是原始数组。

[
  [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
                      ]
                ]
              ]
    ]
  ]
]

预期数组

[
  [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
                      ]
                ]
              ]
    ]
  ]
]

您可以从每个嵌套数组的预期数组中看到一些键值对已被删除。 我需要根据键名过滤数组。 那是我的键名是固定的,需要从其中的每个子数组中删除。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

这是一个基于foreach的递归解决方案的示例,该解决方案使用您提供的数据集处理代码。

    $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);

尽管获取数据很困难,但实现起来却很困难。我确实在此示例中注意到一个“错误”,因为如果键在原始数组中为“ 0”,即使它不在$ keys数组中,它仍然会被删除。

但是我假设该示例足以回答您的问题,并且不会出现我陈述的边缘情况(即,“ 0”不是数组中的键值)。如果确实使用“ 0”作为键,您可以添加其他逻辑来捕获这种情况,但这会使功能放慢一点,因此我将把选择权留给您。

(注意,上面提到的错误已在代码中修复,请参见下面的注释,以获取原始发布者的解决方案)