note 35620 added to function.array-reverse

From: Date: Mon, 08 Sep 2003 16:55:28 +0000
Subject: note 35620 added to function.array-reverse
Groups: php.notes 
Request: Send a blank email to php-notes+get-55856@lists.php.net to get a copy of this message
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"; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.array-reverse.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35620 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35620&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35620&report=yes

« previous php.notes (#55856) next »