首页 > 代码库 > PHP arry_push方法使用注意

PHP arry_push方法使用注意

当在使用arry_push方法向数组中某个子元素中中添加元素时应该注意,如例若使用预定义好的元素push,则会出现以下情况:

这里 "children" => $nodeList 与 array_push($bladeList[0][‘children‘],$nodeList);得到的结果不同!
            $bladeList = array();            $nodeList = array();                        array_push($nodeList, array(                "text" => ‘test’,                "status" =>  ‘1’,                "checked" => false,                "leaf" => true            ));   array_push($bladeList, array(            "text" =>‘testrt’,            "status" =>  ‘1’,            "expanded" => true,            "checked" => false,            "children" => $nodeList            ));   array_push($bladeList[0][‘children‘],$nodeList);

输出结果如下:
0: checked: falsechildren:   0:   checked: false  expanded: true  text: "3"
    children:
//"children" => $nodeList  得到以下结构
    0:     checked: false    leaf: true    status: 0    text: "108"
//
array_push($bladeList[0][‘children‘],$nodeList); 得到以下结构
    1:       0:       checked: false      leaf: true      status: 0      text: "108"

expanded: true
text: "10.0"

 

 

array_push($bladeList[0][‘children‘],$nodeList);

PHP arry_push方法使用注意