|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-12-01 17:56 UTC] alxndr+bugs dot php dot net at gmail dot com
Description:
------------
array_fill() and array_fill_keys() create arrays filled with the same value, with
unique copies in each value. However if that value is a class, it appears that
they are all referencing just one value. That is, modifying a value of an array
created with array_fill/array_fill_keys will modify all the values if that value
is a class. It is as if the new class construction occurs once, and the result is
placed in all values of the array being created (whereas if the value were to be
an integer, string, or array, each value in the created array would be independent
of the others).
Test script:
---------------
$arr1 = array_fill( 0, 2, array('foo') );
print "\narr1 after array_fill() with array as a value:\n".print_r($arr1,true);
array_push($arr1[0],'bar');
print "\narr1 after pushing a string onto the first of the arrays in it:\n".print_r($arr1,true);
class Test {
public $storage = array();
public function __construct($p=null) {
$this->add($p);
}
public function add($p=null) {
if ($p)
$this->storage[] = $p;
}
}
$arr2 = array_fill(0, 2, new Test('foo') );
print "\narr2 after array_fill() with new Test as a value:\n".print_r($arr2,true);
$arr2[0]->add('bar');
print "\narr2 after adding to the first object in it:\n".print_r($arr2,true);
Expected result:
----------------
arr1 after array_fill() with array as a value:
Array
(
[0] => Array
(
[0] => foo
)
[1] => Array
(
[0] => foo
)
)
arr1 after pushing a string onto the first of the arrays in it:
Array
(
[0] => Array
(
[0] => foo
[1] => bar
)
[1] => Array
(
[0] => foo
)
)
arr2 after array_fill() with new Test as a value:
Array
(
[0] => Test Object
(
[storage] => Array
(
[0] => foo
)
)
[1] => Test Object
(
[storage] => Array
(
[0] => foo
)
)
)
arr2 after adding to the first object in it:
Array
(
[0] => Test Object
(
[storage] => Array
(
[0] => foo
[1] => bar
)
)
[1] => Test Object
(
[storage] => Array
(
[0] => foo
)
)
)
Actual result:
--------------
arr1 after array_fill() with array as a value:
Array
(
[0] => Array
(
[0] => foo
)
[1] => Array
(
[0] => foo
)
)
arr1 after pushing a string onto the first of the arrays in it:
Array
(
[0] => Array
(
[0] => foo
[1] => bar
)
[1] => Array
(
[0] => foo
)
)
arr2 after array_fill() with new Test as a value:
Array
(
[0] => Test Object
(
[storage] => Array
(
[0] => foo
)
)
[1] => Test Object
(
[storage] => Array
(
[0] => foo
)
)
)
arr2 after adding to the first object in it:
Array
(
[0] => Test Object
(
[storage] => Array
(
[0] => foo
[1] => bar
)
)
[1] => Test Object
(
[storage] => Array
(
[0] => foo
[1] => bar
)
)
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 23 20:00:01 2025 UTC |
I had a hunch it would be something like that. Out of curiosity, is there a way to do this sort of thing with array_fill? It can be kludged with a foreach*, but it seems that this is what array_fill/array_fill_keys should be for... * e.g. class T { private $t = array(); function add($p) { $this->t[] = $p; } } $a = array_fill(0,2,false); foreach($a as $k=>$v) $a[$k] = new T(); print_r($a); $a[0]->add('foo'); print_r($a);