note 84112 added to function.next

From: Date: Sat, 28 Jun 2008 13:23:53 +0000
Subject: note 84112 added to function.next
Groups: php.notes 
Request: Send a blank email to php-notes+get-140895@lists.php.net to get a copy of this message
@GPatmore and @gigs at gigstaggart dot com: foreach() does not use the real array you are give in... If you want to delete something in the array, the array passed through foreach() has to be a reference to your array... <?php $my_array = explode(',','a,b,c,d,e,f,g'); $ary =& $my_array; // Note: foreach(&ary => $a) wont work! Thats a little bit pity... :( foreach($ary as $a){ print 'letter ' . $a; if(next($ary)){ unset($ary[count($ary) - 1]); print '<br>'; }else{ print '<br>dun!'; } } ?> would finally print: letter a letter b letter c letter d dun! Ohterwise you are able to make a while() as followed: <?php $ary = explode(',','a,b,c,d,e,f,g'); while(list($k,$a) = each($ary)) { print 'letter ' . $a; if(next($ary)){ prev($ary); unset($ary[count($ary) - 1]); print '<br>'; }else{ print '<br>dun!'; } } ?> Consider the prev() after the next()... ;) This is essential for the each(), which means that YOU decide the pointer, while foreach() uses own pointers... ---- Server IP: 217.13.201.10 Probable Submitter: 84.135.114.235 ---- Manual Page -- http://www.php.net/manual/en/function.next.php Edit -- https://master.php.net/note/edit/84112 Del: integrated -- https://master.php.net/note/delete/84112/integrated Del: useless -- https://master.php.net/note/delete/84112/useless Del: bad code -- https://master.php.net/note/delete/84112/bad+code Del: spam -- https://master.php.net/note/delete/84112/spam Del: non-english -- https://master.php.net/note/delete/84112/non-english Del: in docs -- https://master.php.net/note/delete/84112/in+docs Del: other reasons-- https://master.php.net/note/delete/84112 Reject -- https://master.php.net/note/reject/84112 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#140895) next »