如何在Php中的层次数组中增加值?

时间:2016-10-09 15:41:50

标签: php arrays foreach increment hierarchy

大家好,我想知道如何添加增量所有元素?因为到目前为止我不确定在哪里可以包含" inc"在所有元素中

前:

MY_ARRAY = (
[id] => 4
[children] => Array
    (
        [0] => Array
            (
                [id] => 18
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 21
                            )
                        [1] => Array
                            (
                                [id] => 22
                            )
                    )
            )
        [1] => Array
            (
                [id] => 19
            )
        [2] => Array
            (
                [id] => 20
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 26
                            )
                    )
            )
    )
)

使用这些代码:

$in = MY_ARRAY
function generateArray($in, $parent = 0){

        foreach ($in as $key => $value) {
            if(is_numeric($key)){
                $in = $value;
                $out[$key] = $this->generateArray($in, $parent);
            }else{
                $out[$key]=$value;
                if($key=="id"){
                    $out['p_id'] = $parent;
                    $parent=$value;
                }elseif($key=="children"){
                    $in = $value;
                    $out[$key] = $this->generateArray($in, $parent);
                }
            }
        }   
      return $out;
    }

会给我这个输出,不包括[inc]。

[id] => 4
[P_id] => 0
[inc] => 1
[children] => Array
    (
        [0] => Array
            (
                [id] => 18
                [P_id] => 4
                [inc] => 2
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 21
                                [P_id] => 18
                                [inc] => 3
                            )
                        [1] => Array
                            (
                                [id] => 22
                                [P_id] => 18
                                [inc] => 4
                            )
                    )
            )
        [1] => Array
            (
                [id] => 19
                [P_id] => 4
                [inc] => 5
            )

        [2] => Array
            (
                [id] => 20
                [P_id] => 4
                [inc] => 6
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 26
                                [P_id] => 20
                                [inc] => 7
                            )
                    )
            )
    )
)

现在我不确定在哪里以及如何使用上面的代码包含数组中每个元素的[inc]或增量值。

在这里真的需要帮助......

0 个答案:

没有答案