在PHP5中修改多维数组

时间:2016-04-08 00:50:34

标签: php arrays multidimensional-array

我有一个我想要经历的多维数组,并为它添加一个名为“depth”的新键和值,它包含一个整数0及以上。第一级为0,子数组为1,2,依此类推。该数组可以是任何格式的子节点,并继续无休止地链接。我正在弄乱一些预告,但在数组中更深层地声明深键=>值对时遇到了问题。我想修改数组并添加['depth'] =>。

我正在搞乱的代码:

private function recursive($array, $level = 0, $parent = 0) {
    $newlevel = 0;
    $cototal = count($array);

    $arraybuild = array();
    foreach ($array as $key => $value) {

        if (is_numeric($key)) {
            if (is_array($value)) {
                $this->newtree[]=$value;
                $newlevel = $level+1;
                $this->recursive($value, $newlevel, $level);
            }
        }
    }
}

数组的一个例子是:

Array (
[0] => Array
(
    [name] => Whatwwww
    [cat_id] => 6
)

[1] => Array
(
    [name] => 43adsfasdfd
    [cat_id] => 5
    [children] => Array
    (
        [0] => Array
        (
            [name] => AAAAAAA
            [cat_id] => 7
            [children] => Array
            (
                [0] => Array
                (
                    [name] => CCCCCCCCCCCC
                    [cat_id] => 9
                )

            )

        )

        [1] => Array
        (
            [name] => bbbbbb
            [cat_id] => 8
        )

    )
)

[2] => Array
(
    [name] => Test Category
    [cat_id] => 1
    [children] => Array
    (
        [0] => Array
        (
            [name] => asdfasdfd
            [cat_id] => 4
        )

        [1] => Array
        (
            [name] => yetstes1
            [cat_id] => 2
        )
    )
)
)

将数组修改为如下所示:

    Array (
    [0] => Array
    (
        [name] => Whatwwww
        [cat_id] => 6
        [depth] => 0
    )

    [1] => Array
    (
        [name] => 43adsfasdfd
        [cat_id] => 5
        [depth] => 0
        [children] => Array
        (
            [0] => Array
            (
                [name] => AAAAAAA
                [cat_id] => 7
                [depth] => 1
                [children] => Array
                (
                    [0] => Array
                    (
                        [name] => CCCCCCCCCCCC
                        [cat_id] => 9
                        [depth] => 2
                    )

                )

            )

            [1] => Array
            (
                [name] => bbbbbb
                [cat_id] => 8
                [depth] => 1
            )

        )
    )

1 个答案:

答案 0 :(得分:1)

试一试。您需要根据具体的代码库进行调整,但这样做有效:

function addDepth_r(&$array, $level = 0) {

    foreach ($array as $k => $v) {      

        if (is_array($array[$k])) {
            $level++;
            addDepth_r($array[$k], $level);
        } else if(!isset($array['depth'])) {
            $array['depth'] = $level;
        }
    }
}

$a = array(

    array(
        'name' => 'Whatwwww',
        'cat_id' => 6
    ),

    array(
        'name' => '43adsfasdfd',
        'cat_id' => 5,
        'children' => array(
            array(
                'name' => 'AAAAAAA',
                'cat_id' => 7,
                'children' => array(
                    array(
                        'name' => 'CCCCCCCCCCCC',
                        'cat_id' => 9,
                    )

                )

            ),

            array(
                'name' => 'bbbbbb',
                'cat_id' => 8,
            )

        )
    )
);

echo '<pre>Initial array:<br>';
print_r($a);
echo '<br><br><br>Processed array:<br>';
addDepth_r($a);
print_r($a);
echo '</pre>';

如果您希望每个级别都标有深度,只需重新排列功能:

function addDepth_r(&$array, $level = 0) {

    foreach ($array as $k => $v) {      
        if(!isset($array['depth'])) {
            $array['depth'] = $level;
        }
        if (is_array($array[$k])) {
            $level++;
            addDepth_r($array[$k], $level);
        }
    }
}