如何从多维数组生成索引的平面数组?

时间:2014-04-03 06:51:02

标签: php arrays recursion multidimensional-array

我有来自PHP的以下数组:

Array
(
    [0] => Array
        (
            [ContractExhibitsData] => Array
                (
                    [id] => 2
                    [exhibit_id] => 2
                    [parent_id] => 
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [ContractExhibitsData] => Array
                                (
                                    [id] => 98
                                    [exhibit_id] => 2
                                    [parent_id] => 2
                                )

                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [ContractExhibitsData] => Array
                                                (
                                                    [id] => 99
                                                    [exhibit_id] => 2
                                                    [parent_id] => 98
                                                )

                                            [children] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [ContractExhibitsData] => Array
                                                (
                                                    [id] => 100
                                                    [exhibit_id] => 2
                                                    [parent_id] => 98
                                                )

                                            [children] => Array
                                                (
                                                )

                                        )
                                    )
                                )
                            )
                        )
                    );

它本质上是一个树数组,由具有更多数据的子节点的节点组成。我想使用递归函数迭代这个数组,以生成如下数组:

$return = Array
(
    [2] => '1.',
    [98] => '1.1.',
    [99] => '1.1.1.',
    [100] => '1.1.2.'
);

等等。基本上,它将是一组键值对,其中数组键是ContractExhibitsData中元素的“id”,值是数字索引。

我有以下功能,我一直在修补,但它并没有让我到达我需要的地方。

private function getIndexes($data, $prefix = null) {

    $indexes = array();
    $count   = 0;

    if ($prefix) {
        $prefix = $prefix . '.';
    }

    if (!empty($data['children'])) {
        foreach($data['children'] as $child) {
            $count++;

            $indexes[$child['ContractExhibitsData']['id']] = $prefix.$count;

            if (is_array($child['children']) && !empty($child['children'])) {
                $subIndex = $this->getIndexes($child, $prefix.$count);
                return $subIndex;
            }
        }
    }

    return $indexes;

}

1 个答案:

答案 0 :(得分:0)

知道了!请参阅工作版here

<?php

function getIndexes($data, $prefix = '') {

    $indexes = array();
    $count   = 1;

    if (!empty($prefix)) {
        $prefix = $prefix . '.';
    }

    foreach ($data as $dataItem) {
        $item   = $dataItem['ContractExhibitsData'];
        $index  = $prefix.$count;

        $indexes[$item['id']] = $index;

        if(!empty($dataItem['children'])) {
            $indexes = array_merge_recursive($indexes,getIndexes($dataItem['children'],$index));
        }
        $count++;
    }

    return $indexes;

}

$yourArray = array
(
    0 => array (
        'ContractExhibitsData' => array
        (
            'id' => 2,
            'exhibit_id' => 2,
            'parent_id' => null
        ),
        'children' => array
        (
            0 => array
            (
                'ContractExhibitsData' => array
                (
                    'id' => 98,
                    'exhibit_id' => 2,
                    'parent_id' => 2
                ),
                'children' => array
                (
                    0 => array
                    (
                        'ContractExhibitsData' => array
                        (
                            'id' => 99,
                            'exhibit_id' => 2,
                            'parent_id' => 98
                        ),
                        'children' => array()

                    ),
                    1 => array
                    (
                        'ContractExhibitsData' => array
                        (
                            'id' => 100,
                            'exhibit_id' => 2,
                            'parent_id' => 98
                        ),
                        'children' => array ()
                    )
                )
            )
        )
    )
);

$result = getIndexes($yourArray);
var_dump($result);

你的一个问题是你的循环中有一个返回,从而打破你的循环。此外,您将null添加为字符串,因为这是$前缀的默认设置。你从来没有迭代过初始数组,但总是只是孩子们。