<?php
/**
* Replaces elements from passed arrays into the first array
*
* array_replace() replaces the values of array1 with values having the
* same keys in each of the following arrays. If a key from the first array
* exists in the second array, its value will be replaced by the value from
* the second array. If the key exists in the second array, and not the
* first, it will be created in the first array. If a key only exists in
* the first array, it will be left as is. If several arrays are passed for
* replacement, they will be processed in order, the later arrays
* overwriting the previous values.
*
* @since PHP 5.3.0
* @param array $array1 The array in which elements are replaced
* @param array $array2... The array from which elements will be extracted
* @return array Returns an array, or NULL if an error occurs
*/
function phpcompat_array_replace(array $array1, array $array2 = null)
{
$args = func_get_args();
$num_args = func_num_args();
if ($num_args < 1) {
trigger_error(
sprintf('array_replace() expects at least 1 parameter, %d given', $num_args),
E_USER_WARNING
);
return null;
}
$res = array();
for ($i = 0; $i < $num_args; ++$i) {
if (is_array($args[$i])) {
foreach ($args[$i] as $key => $val) {
$res[$key] = $val;
}
} else {
trigger_error(
sprintf('array_replace(): Argument #%d is not an array', $i + 1),
E_USER_WARNING
);
return null;
}
}
return $res;
}
var_dump(array_replace());
var_dump(phpcompat_array_replace());
Fatal error: Uncaught ArgumentCountError: array_replace() expects at least 1 argument, 0 given in /in/kX30W:53
Stack trace:
#0 /in/kX30W(53): array_replace()
#1 {main}
thrown in /in/kX30W on line 53
Process exited with code 255.
Warning: array_replace() expects at least 1 parameter, 0 given in /in/kX30W on line 53
NULL
Fatal error: Uncaught ArgumentCountError: Too few arguments to function phpcompat_array_replace(), 0 passed in /in/kX30W on line 54 and at least 1 expected in /in/kX30W:21
Stack trace:
#0 /in/kX30W(54): phpcompat_array_replace()
#1 {main}
thrown in /in/kX30W on line 21
Process exited with code 255.
Output for 7.0.0 - 7.0.33
Warning: array_replace() expects at least 1 parameter, 0 given in /in/kX30W on line 53
NULL
Fatal error: Uncaught TypeError: Argument 1 passed to phpcompat_array_replace() must be of the type array, none given, called in /in/kX30W on line 54 and defined in /in/kX30W:21
Stack trace:
#0 /in/kX30W(54): phpcompat_array_replace()
#1 {main}
thrown in /in/kX30W on line 21
Process exited with code 255.
Warning: array_replace() expects at least 1 parameter, 0 given in /in/kX30W on line 53
NULL
Catchable fatal error: Argument 1 passed to phpcompat_array_replace() must be of the type array, none given, called in /in/kX30W on line 54 and defined in /in/kX30W on line 21
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Warning: array_replace() expects at least 1 parameter, 0 given in /in/kX30W on line 53
NULL
Catchable fatal error: Argument 1 passed to phpcompat_array_replace() must be an array, none given, called in /in/kX30W on line 54 and defined in /in/kX30W on line 21
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Fatal error: Call to undefined function array_replace() in /in/kX30W on line 53
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/kX30W on line 21
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_ARRAY, expecting ')' in /in/kX30W on line 21
Process exited with code 255.