note 124505 deleted from function.array-fill-keys by cmb

From: Date: Sat, 14 Dec 2019 16:38:38 +0000
Subject: note 124505 deleted from function.array-fill-keys by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213514@lists.php.net to get a copy of this message
Note Submitter: Radon8472 ---- This function has a undocumented behavoir when you use objects as value. When you use objects as $value for array_fill_keys each entry of the new array is a reference to the same object. For this example-code, I expeced that each entry contains only one entry in "loop". With the "array"-test this worked, but in the object-test not. <?php $keys = array("step1","step2"); $tests = array( "array" => array_fill_keys($keys, array()), "object"=> array_fill_keys($keys, new ArrayObject()) ); foreach($tests as $testName => $aData) { foreach($aData as $k => &$v) { $v["loops"][] = $k; } echo "Results for ".$testName."\n"; print_r($aData); } ?> But the Output was: --------------------------------- Results for native_array Array ( [step1] => Array ( [loops] => Array ( [0] => step1 ) ) [step2] => Array ( [loops] => Array ( [0] => step2 ) ) ) Results for object Array ( [step1] => ArrayObject Object ( [storage:ArrayObject:private] => Array ( [loops] => Array ( [0] => step1 [1] => step2 ) ) ) [step2] => ArrayObject Object ( [storage:ArrayObject:private] => Array ( [loops] => Array ( [0] => step1 [1] => step2 ) ) ) ) --------------------------------- You can test it in multipe php-versions here: https://3v4l.org/TpP3N

« previous php.notes (#213514) next »