note 95056 modified in function.array-replace by danbrown

From: Date: Fri, 11 Dec 2009 16:21:49 +0000
Subject: note 95056 modified in function.array-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-163863@lists.php.net to get a copy of this message
To get exactly same result like in PHP 5.3, the foreach loop in your code should look like: <?php ... $count = func_num_args(); for ($i = 1; $i < $count; $i++) { ... } ... ?> Check on this code: <?php $base = array('id' => NULL, 'login' => NULL, 'credit' => NULL); $arr1 = array('id' => 2, 'login' => NULL, 'credit' => 5); $arr2 = array('id' => NULL, 'login' => 'john.doe', 'credit' => 100); $result = array_replace($base, $arr1, $arr2); /* correct output: array(3) { "id" => NULL "login" => string(8) "john.doe" "credit" => int(100) } your output: array(3) { "id" => int(2) "login" => NULL "credit" => int(5) } */ ?> Function array_replace "replaces elements from passed arrays into the first array" -- this means replace from top-right to first, then from top-right - 1 to first, etc, etc... --was-- Sorry my mistake - wrong interpretation, late night, nevermind... This should work exactly same as in PHP 5.3: <?php ... $count = func_num_args(); for ($i = 1; $i < $count; $i++) { ... } ... ?> Check on this code: <?php $base = array('id' => NULL, 'login' => NULL, 'credit' => NULL); $arr1 = array('id' => 2, 'login' => NULL, 'credit' => 5); $arr2 = array('id' => NULL, 'login' => 'john.doe', 'credit' => 100); $result = array_replace($base, $arr1, $arr2); /* correct output: array(3) { "id" => NULL "login" => string(8) "john.doe" "credit" => int(100) } your output: array(3) { "id" => int(2) "login" => NULL "credit" => int(5) } */ ?> http://php.net/manual/en/function.array-replace.php

« previous php.notes (#163863) next »