PHP遍历函数将单个数组转换为带有子元素的嵌套数组 - 基于父ID [英] PHP Traversing Function to turn single array into nested array with children - based on parent id

查看:34
本文介绍了PHP遍历函数将单个数组转换为带有子元素的嵌套数组 - 基于父ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似这样的数组:

数组(大批([ID] =>1[parentcat_ID] =>0),大批([ID] =>2[parentcat_ID] =>0),大批([ID] =>6[parentcat_ID] =>1),大批([ID] =>7[parentcat_ID] =>1),大批([ID] =>8[parentcat_ID] =>6),大批([ID] =>9[parentcat_ID] =>1),大批([ID] =>13[parentcat_ID] =>7),大批([ID] =>14[parentcat_ID] =>8))

但我需要一个函数来递归地将每个项目放入相关父数组内的子"数组中.所以它看起来更像这样:

数组(大批([ID] =>1[parentcat_ID] =>0[儿童] =>大批 (大批([ID] =>6[parentcat_ID] =>1[儿童] =>大批 (大批([ID] =>8[parentcat_ID] =>6[儿童] =>大批 (大批([ID] =>14[parentcat_ID] =>8))))),大批([ID] =>7[parentcat_ID] =>1[儿童] =>大批(大批([ID] =>13[parentcat_ID] =>7))),大批([ID] =>9[parentcat_ID] =>1)))大批([ID] =>2[parentcat_ID] =>0))

我希望这是有道理的!

解决方案

试试这个(在 php 5.2 下测试):

<前>$inArray = 数组(数组('ID' => '1', 'parentcat_ID' => '0'),数组('ID' => '2', 'parentcat_ID' => '0'),数组('ID' => '6', 'parentcat_ID' => '1'),数组('ID' => '7', 'parentcat_ID' => '1'),数组('ID' => '8', 'parentcat_ID' => '6'),数组('ID' => '9', 'parentcat_ID' => '1'),数组('ID' => '13', 'parentcat_ID' => '7'),数组('ID' => '14', 'parentcat_ID' => '8'),);函数 makeParentChildRelations(&$inArray, &$outArray, $currentParentId = 0) {if(!is_array($inArray)) {返回;}if(!is_array($outArray)) {返回;}foreach($inArray as $key => $tuple) {if($tuple['parentcat_ID'] == $currentParentId) {$tuple['children'] = array();makeParentChildRelations($inArray, $tuple['children'], $tuple['ID']);$outArray[] = $tuple;}}}$outArray = array();makeParentChildRelations($inArray, $outArray);打印_r($outArray);

I have an array similar to this:

Array
(
    Array
    (
        [ID] => 1
        [parentcat_ID] => 0
    ),
    Array
    (
        [ID] => 2
        [parentcat_ID] => 0
    ),
    Array
    (
        [ID] => 6
        [parentcat_ID] => 1
    ),
    Array
    (
        [ID] => 7
        [parentcat_ID] => 1
    ),
    Array
    (
        [ID] => 8
        [parentcat_ID] => 6
    ),
    Array
    (
        [ID] => 9
        [parentcat_ID] => 1
    ),
    Array
    (
        [ID] => 13
        [parentcat_ID] => 7
    ),
    Array
    (
        [ID] => 14
        [parentcat_ID] => 8
    )

)

But I need a function to recursively put each item into a 'children' array inside the relevant parent array. So it would look more like this:

Array
(
    Array
    (
        [ID] => 1
        [parentcat_ID] => 0
        [children] => Array (
            Array
            (
                [ID] => 6
                [parentcat_ID] => 1
                [childen] => Array (
                    Array
                    (
                        [ID] => 8
                        [parentcat_ID] => 6
                        [children] => Array (
                             Array
                             (
                                 [ID] => 14
                                 [parentcat_ID] => 8
                             )
                        )
                    )
                )
            ),
            Array
            (
                [ID] => 7
                [parentcat_ID] => 1
                [children] => Array(
                     Array
                     (
                         [ID] => 13
                         [parentcat_ID] => 7
                     )
                ) 
            ),
            Array
            (
                [ID] => 9
                [parentcat_ID] => 1
            )

        )
    )
    Array
    (
        [ID] => 2
        [parentcat_ID] => 0

    )

)

I hope that makes sense!

解决方案

Give this a go (tested under php 5.2):

$inArray = array(
    array('ID' => '1', 'parentcat_ID' => '0'),
    array('ID' => '2', 'parentcat_ID' => '0'),
    array('ID' => '6', 'parentcat_ID' => '1'),  
    array('ID' => '7', 'parentcat_ID' => '1'),
    array('ID' => '8', 'parentcat_ID' => '6'),          
    array('ID' => '9', 'parentcat_ID' => '1'),  
    array('ID' => '13', 'parentcat_ID' => '7'),
    array('ID' => '14', 'parentcat_ID' => '8'),     
);

function makeParentChildRelations(&$inArray, &$outArray, $currentParentId = 0) {
    if(!is_array($inArray)) {
        return;
    }

    if(!is_array($outArray)) {
        return;
    }

    foreach($inArray as $key => $tuple) {
        if($tuple['parentcat_ID'] == $currentParentId) {
            $tuple['children'] = array();
            makeParentChildRelations($inArray, $tuple['children'], $tuple['ID']);
            $outArray[] = $tuple;   
        }
    }
}

$outArray = array();
makeParentChildRelations($inArray, $outArray);

print_r($outArray);

这篇关于PHP遍历函数将单个数组转换为带有子元素的嵌套数组 - 基于父ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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