note 115753 added to control-structures.foreach

From: Date: Wed, 17 Sep 2014 23:23:10 +0000
Subject: note 115753 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-200468@lists.php.net to get a copy of this message
Not sure if this is considered a bug <?php $array = ['numbers' => [1, 2, 3]]; foreach ($array as &$numbers) { $numbers = [4, 5, 6]; } var_dump($array); modify_array($array); var_dump($array); function modify_array($array_arg) { $array_arg['numbers'] = [1, 2, 3]; } Prints array(1) { ["numbers"]=> &array(3) { [0]=> int(4) [1]=> int(5) [2]=> int(6) } } array(1) { ["numbers"]=> &array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } } ?> Notice that $array was not passed by reference to modify_array() however the nested array was modified. It sounds logical but not sure if this is documented! ---- Server IP: 72.52.91.14 Probable Submitter: 77.172.234.84 ---- Manual Page -- http://php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/115753 Del: integrated -- https://master.php.net/note/delete/115753/integrated Del: useless -- https://master.php.net/note/delete/115753/useless Del: bad code -- https://master.php.net/note/delete/115753/bad+code Del: spam -- https://master.php.net/note/delete/115753/spam Del: non-english -- https://master.php.net/note/delete/115753/non-english Del: in docs -- https://master.php.net/note/delete/115753/in+docs Del: other reasons-- https://master.php.net/note/delete/115753 Reject -- https://master.php.net/note/reject/115753 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#200468) next »