【问题标题】:How to unset every array in nested foreach?如何取消设置嵌套 foreach 中的每个数组?
【发布时间】:2020-05-01 20:22:00
【问题描述】:

我有一个这样的多维数组输出:

    Array
(
    [0] => Array
        (
            [item] => null
            [count] => 0
            [child] => Array
                (
                    [Dagadu Bocah] => Array
                        (
                            [item] => Dagadu Bocah
                            [count] => 47
                            [child] => Array
                                (
                                    [HirukPikuk] => Array
                                        (
                                            [item] => HirukPikuk
                                            [count] => 5
                                            [child] => Array
                                                (
                                                    [DGD] => Array
                                                        (
                                                            [item] => DGD
                                                            [count] => 1
                                                            [child] => 
                                                        )

                                                )

                                        )

在我的预期中,我可以在 foreach 循环中使用 unset 函数来删除每个具有 3 个键的数组,即 'item'、'count' 和 'child' 以便它生成一个像这样的数组:

      Array
(
    [0] => ([Dagadu Bocah] =>Array([HirukPikuk] =>Array([DGD])

                                        )

这是我的代码,不符合我的期望:

    public function conditionalPatternBase($a){
{  
  foreach($a as $key => $value){
    foreach($value['child'] as $key1 => $value1){
        if(is_array($value1['child'])){
          foreach($value1['child'] as $value2){
            unset($value2);
            if(is_array($value2['child'])){
              foreach($value2['child'] as $value3){
                unset($value3);
                if(is_array($value3['child'])){
                  foreach($value3['child'] as $value4){
                    unset($value4);
                    if(is_array($value4['child'])){
                      foreach($value4['child'] as $value5){
                        unset($value5);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

}

谁能帮忙?

【问题讨论】:

    标签: php arrays multidimensional-array unset


    【解决方案1】:

    对于你想要使用recursion的那种数据,像这样:

    <?php
    function getChildKey(array $data)
    {
        $result = [];
    
        if (isset($data['child'])) {
            foreach ($data['child'] as $key => $value) {
                $result[$key] = getChildKey($value);
            }
        }
    
        if (empty($result)) {
            return '';
        }
    
        return $result;
    }
    
    $input = [
        [
            'item'  => null,
            'count' => 0,
            'child' => [
                'Dagadu Bocah' => [
                    'item'  => 'Dagadu Bocah',
                    'count' => 47,
                    'child' => [
                        'HirukPikuk' => [
                            'item'  => 'HirukPikuk',
                            'count' => 5,
                            'child' => [
                                'DGD' => [
                                    'item'  => 'DGD',
                                    'count' => 1,
                                    'child' => null,
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ];
    
    $output = [];
    print_r($input);
    
    foreach ($input as $index => $child) {
        $output[$index] = getChildKey($child);
    }
    
    print_r($output);
    

    【讨论】:

      猜你喜欢
      • 2020-05-02
      • 2020-01-31
      • 1970-01-01
      • 2021-12-05
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      相关资源
      最近更新 更多