bingjie2680
bingjie2680

Reputation: 7773

php an array of reference

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 assigned by reference 

what am i doing wrong here? thanks for help.

Upvotes: 0

Views: 159

Answers (2)

dynamic
dynamic

Reputation: 48091

Edited

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

or

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

Upvotes: 1

Gedrox
Gedrox

Reputation: 3612

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;

Upvotes: 0

Related Questions