开发者

php an array of reference

开发者 https://www.devze.com 2023-03-18 06:56 出处:网络
I have two arrays. one will be added with arrays as children, another one keeps references to the array being added to the first one.

I have two arrays. one will be added with arrays as children, another one keeps references to the array being added to the first one.

$list=array();
$stack=array();

in a for loop:

$list[]=array('something');
$stack[]=& end($list); //errors: Only variables should be as开发者_开发百科signed by reference 

what am i doing wrong here? thanks for help.


Edited

$stack[] = &$list[count($list)-1];  //> Assuming numeric index incremental

or

end($list);
$stack[] = &$list[key($list)];


Objects will be always passed by reference in PHP 5.

EDITED

But if array is not a class then

$element = array();
$list[] = &$element;
$stack[] = &$element;
0

精彩评论

暂无评论...
验证码 换一张
取 消