dongyinzheng6572 2017-10-19 13:35
浏览 77
已采纳

使用PHP可变级别多维数组来'flatten'数组? [重复]

This question already has an answer here:

I'm looking for a solution to flatten a variable multi-dimensionaled array to flatten all the values in the last available array to a single Array[] (containing the dataset of each last array in the multidimensional array).

Any help is appreciated!

It differs from other questions

Because the ending result should be a collection of all Arrays containing :

array(
    'title' => xx,
    'id' => xx
)

listed in the different multidimensional arrays. The different items all have fixed keys: title and id.

Sample Base array

$data = array(
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        array(
            array(
                'title' => xx,
                'id' => xx
            ),
            array(
                'title' => xx
                'id' => xx
            )
        )
    ),
    array(
        array(
            array(
                array(
                    'title' => xx,
                    'id' => xx
                ),
                array(
                    'title' => xx,
                    'id' => xx
                ),
                array(
                    'title' => xx,
                    'id' => xx
                )
            )
        ),
        array(
            'title' => xx,
            'id' => xx
        ),
        array(
            array(
                array(
                    array(
                        'title' => xx,
                        'id' => xx
                    )
                )
            )
        )
    )
);

Should be flattend to

 $data = array(
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    ),
    array(
        'title' => xx,
        'id' => xx
    )
);
</div>
  • 写回答

1条回答 默认 最新

  • duancuan7057 2017-10-19 14:03
    关注
    function flatten_array(&$data) {
    
        foreach ($data as $index => &$item) {
    
            if(has_array_child($item)) {
                unset($data[$index]);
                flatten_array($item);
                $data = array_merge($data, $item);
            }
    
        }
    
    }
    
    function has_array_child($item) {
        foreach($item as $child) {
            if(is_array($child)) {
                return TRUE;
            }
        }
    
        return FALSE;
    }
    
    flatten_array($data);
    
    print_r($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?