tags:

views:

54

answers:

2

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..

A: 
<?php
$base = array("orange", "banana", "apple", "raspberry");
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");

$basket = array_replace($base, $replacements, $replacements2);
print_r($basket);
?>

below one is working fine.

$arr2 = array('id' => 12, 'login' => 'john.doe', 'credit' => 100); print_r($arr2); $arr2['id']='bharani'; print_r($arr2);

Bharanikumar
Please delete this post, edit your question and put the code there instead. It is not an answer! If your are implicitly saying that the first snippet does not work (please talk to us!) then it seems you don't have PHP 5.3 .
Felix Kling
Someone up-voted this? Seriously?
Felix Kling
+4  A: 
<?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?

maggie