note 35620 deleted from function.array-reverse by salathe

From: Date: Fri, 08 Jun 2012 20:09:16 +0000
Subject: note 35620 deleted from function.array-reverse by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188506@lists.php.net to get a copy of this message
Note Submitter: Michiel de Roo ---- If you need to reverse an array by reference, you can use the following function. <? function &array_reverse_ref(&$a) { $r = array(); for($i=0, $j=count($a); $i<count($a); $i++, $j--) { $r[$i] =& $a[$j-1]; } return $r; } ?> Use it like: <? class t { var $message = "message"; } for($i=0; $i<10; $i++) { $a[] = new t(); } $ra =& array_reverse_ref($a); $a[0]->message = "now... this works in both"; $ra[0]->message = "but.... does not work in both"; for($i=0; $i<10; $i++) { echo $ra[$i]->message; echo "<br>\n"; } echo "<br>\n"; for($i=0; $i<10; $i++) { echo $a[$i]->message; echo "<br>\n"; } ?>

« previous php.notes (#188506) next »