note 73740 added to function.array-splice
| From: | mip at ycn dot com | Date: | Thu, 08 Mar 2007 13:37:29 +0000 |
| Subject: | note 73740 added to function.array-splice | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124233@lists.php.net to get a copy of this message | ||
Ever wounder what array_splice is doing to your references, then try this little script and see the
output.
<?php
$a = "a";
$b = "b";
$c = "c";
$d = "d";
$arr = array();
$arr[] =& $a;
$arr[] =& $b;
$arr[] =& $c;
array_splice($arr,1,0,array($d));
$sec_arr = array();
$sec_arr[] =& $d;
array_splice($arr,1,0,$sec_arr);
$arr[0] = "test"; // should be $a
$arr[3] = "test2"; // should be $b
$arr[1] = "this be d?"; // should be $d
$arr[2] = "or this be d?"; // should be $d
var_dump($arr);
var_dump($a);
var_dump($b);
var_dump($d);
?>
The output will be (PHP 4.3.3):
array(5) {
[0]=>
&string(4) "test"
[1]=>
&string(10) "this be d?"
[2]=>
string(13) "or this be d?"
[3]=>
&string(5) "test2"
[4]=>
&string(1) "c"
}
string(4) "test"
string(5) "test2"
string(10) "this be d?"
So array_splice is reference safe, but you have to be careful about the generation of the
replacement array.
have fun, cheers!
----
Server IP: 69.147.83.197
Probable Submitter: 213.47.65.155
----
Manual Page -- http://www.php.net/manual/en/function.array-splice.php
Edit -- https://master.php.net/note/edit/73740
Del: integrated -- https://master.php.net/note/delete/73740/integrated
Del: useless -- https://master.php.net/note/delete/73740/useless
Del: bad code -- https://master.php.net/note/delete/73740/bad+code
Del: spam -- https://master.php.net/note/delete/73740/spam
Del: non-english -- https://master.php.net/note/delete/73740/non-english
Del: in docs -- https://master.php.net/note/delete/73740/in+docs
Del: other reasons-- https://master.php.net/note/delete/73740
Reject -- https://master.php.net/note/reject/73740
Search -- https://master.php.net/manage/user-notes.php