note 116889 added to control-structures.foreach

From: Date: Mon, 16 Mar 2015 03:49:46 +0000
Subject: note 116889 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-201954@lists.php.net to get a copy of this message
@tedivm at tedivm dot com With foreach you can specify the & for $value. In this case you modify the original array. Otherwise you operate on a "copy" (not on the original array). Consider the following example, that is identical with yours, except i use the & for $value: $arr = array(1,2,3,4,5,6,7,8,9); foreach($arr as $key=>&$value) { unset($arr[$key + 1]); echo "\n\n--------\nKey: $key\nValue: $value\n"; var_dump($arr); echo "--------"; } echo "\n\nARR at the end:\n"; var_dump($arr); In foreach it prints the following values: 1, 3, 5, 7, 9 And you can see, that in the loop the corresponding elements are removed from arr. So at the end you'll have an array, that contains only 1, 3, 5, 7, 9. ---- Server IP: 72.52.91.14 Probable Submitter: 86.33.71.83 ---- Manual Page -- http://php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/116889 Del: integrated -- https://master.php.net/note/delete/116889/integrated Del: useless -- https://master.php.net/note/delete/116889/useless Del: bad code -- https://master.php.net/note/delete/116889/bad+code Del: spam -- https://master.php.net/note/delete/116889/spam Del: non-english -- https://master.php.net/note/delete/116889/non-english Del: in docs -- https://master.php.net/note/delete/116889/in+docs Del: other reasons-- https://master.php.net/note/delete/116889 Reject -- https://master.php.net/note/reject/116889 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#201954) next »