note 48730 added to language.types.array

From: Date: Wed, 05 Jan 2005 15:06:36 +0000
Subject: note 48730 added to language.types.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-82789@lists.php.net to get a copy of this message
Attention with Arrays in Arrays! If you copy (=) an array which contains arrays it will be REFERENCED not COPIED. Example: <?php /* GOOD ONE */ echo "<b>Here copy (=) works correct:</b><br>"; /* Initialise Array 1 */ $x1 = array(array(10,20),array(30,40)); /* COPY Array */ $x2 = $x1; /* Change some values in Array 2 */ $x2[0][0]=77; $x2[1][1]=99; echo "<b>Original:</b><pre>"; var_dump($x1); echo "</pre><b>Changed Copy:</b><pre>"; var_dump($x2); /* BAAAAAAAD ONE */ echo "</pre><hr><b>Here copy (=) FAILS:</b><br>"; /* Initialise Array 1 */ $a1[0]->bla[0]->id=10; $a1[0]->bla[1]->id=20; $a1[1]->bla[0]->id=30; $a1[1]->bla[1]->id=40; /* COPY Array */ $a2 = $a1; /* Change some values in Array 2 (!) */ $a2[0]->bla[0]->id=77; $a2[1]->bla[1]->id=99; echo "<b>Original:</b><pre>"; var_dump($a1); echo "</pre><b>Changed Copy:</b><pre>"; var_dump($a2); echo "</pre>"; php?> The output of $a1 and $a2 will be the same.. ---- Manual Page -- http://www.php.net/manual/en/language.types.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48730 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48730&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48730&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48730&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48730&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48730&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48730&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48730&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#82789) next »