|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-10-23 10:31 UTC] johannes@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 29 23:00:01 2025 UTC |
Description: ------------ When a member of a class(let's say class A)is an array of objects from other class B (who has a member that is an array too) and i want to populate the $this->member from the class A using $this->member[]=$some_object_of_class_b it does increase the number of elements in the array BUT IT OVERWRITES ALL ELEMENTS VALUE OF THE $this->member ARRAY. Reproduce code: --------------- class A{ public $arry = array(); public function set_array($array){ $this->arry = $array;} } class B{ public $array_of_object; public function add_object($object){ $this->array_of_object[] = $object; } } $array = array('I' => 'you'); $a = new A(); $b = new B(); for($i=0 ; $i < 3 ; $i++){ $array1 = array($i=>'he'); $array = array_merge($array, $array1); $a->set_array($array); $b->add_object($a);} var_dump($b); Expected result: ---------------- object(B)#2 (1) { ["array_of_object"]=> array(3) { [0]=> object(A)#1 (1) { ["arry"]=> array(2) { ["I"]=> string(3) "you" [0]=> string(2) "he" } } [1]=> object(A)#1 (1) { ["arry"]=> array(3) { ["I"]=> string(3) "you" [0]=> string(2) "he" [1]=> string(2) "he" } } [2]=> object(A)#1 (1) { ["arry"]=> array(4) { ["I"]=> string(3) "you" [0]=> string(2) "he" [1]=> string(2) "he" [2]=> string(2) "he" } } } } Actual result: -------------- object(B)#2 (1) { ["array_of_object"]=> array(3) { [0]=> object(A)#1 (1) { ["arry"]=> array(4) { ["I"]=> string(3) "you" [0]=> string(2) "he" [1]=> string(2) "he" [2]=> string(2) "he" } } [1]=> object(A)#1 (1) { ["arry"]=> array(4) { ["I"]=> string(3) "you" [0]=> string(2) "he" [1]=> string(2) "he" [2]=> string(2) "he" } } [2]=> object(A)#1 (1) { ["arry"]=> array(4) { ["I"]=> string(3) "you" [0]=> string(2) "he" [1]=> string(2) "he" [2]=> string(2) "he" } } } }