如何取消嵌套的foreach中的每个数组? [英] How to unset every array in nested foreach?

查看:89
本文介绍了如何取消嵌套的foreach中的每个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a piece of multidimensional array output like this:

    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")的数组,以便产生一个像这样的数组:

In my expectations, I can use the unset function in the foreach loop to delete each array that has 3 keys, namely 'item', 'count' and 'child' so that it produces an array like this:

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

                                        )

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

this is my code and does not meet my expectations:

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

}

任何人都可以帮忙吗?

推荐答案

对于您要使用的此类数据

For that kind of data you want to use recursion, like this:

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

这篇关于如何取消嵌套的foreach中的每个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆