is there any array replace function in php ..
in php manual there is a function called array_replace but its not working...
Tell me some example...
for array replace..
<?php
$base = array("orange", "banana", "apple", "raspberry");
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");
$basket = array_replace($base, $replacements, $replacements2);
echo '<pre>' .
print_r($base, true) .
print_r($basket, true) .
'</pre>';
output
Array
(
[0] => orange
[1] => banana
[2] => apple
[3] => raspberry
)
Array
(
[0] => grape
[1] => banana
[2] => apple
[3] => raspberry
[4] => cherry
)
the question is: what exactly isnt working for you?
There's a much easier way. Dereferencing:
$myarray = array("Peter", "Brian", "Stewart");
$myarray['2'] = "Stewie";
print_r($myarray);
Output:
Array
(
[0] => Peter
[1] => Brian
[2] => Stewie
)
I can't remember where I found this solution so I cannot give credit to the person who created this alternative array_replace function for Servers who do not have the array_replace function available. It works great and is seamless to add since you would not need to change your existing code that is using the array_replace function.
Edit: found the author. Credit goes to MikevHoenselaar: https://github.com/MikeRogers0/PHP-HTML5-Form-class/issues/4
// Fallback function if the PHP Server does not have the array_replace function
if ( !function_exists('array_replace') ) {
function array_replace() {
$array = array();
$n = func_num_args();
while ( $n-- >0 ) {
$array+=func_get_arg($n);
}
return $array;
}
}