my life

day to day

Archive for June 13th, 2006

PHP array_shift pass-by-reference

Tuesday, June 13th, 2006

If $list is an Array of references, and you wish to “shift” off the first reference, you cannot simply do


$obj 
array_shift$list );

it will return by value $obj.

The roundabout way of doing it is:


$obj 
= &$list[0];
array_shift$list ) ;


or alternately,


reset
($list);
$obj = &$listkey($list) ];
array_shift$list );


The same goes for array_pop, just reverse the logic for the end of the Array.

add to del.icio.us    add to technorati favs   email this