is this possible

Hello everyone
i wonder if there is a way in php that enables me to merge 2 arrays replacing the similar values
what i mean is this
[php]<
$array1 = array( 0 => 10, 1 => 20);
$array2 = array( 0 => 20, 2 => 30);
$merge = array_merge_recursive($array1, $array2);
print_r($merge);
[/php]
the output of that would be :
Array ( [0] => 10 [1] => 20 [2] => 20 [3] => 30 )
is there a way that i can make it something like :
Array ( [0] => 10 [1] => 20 [2] => 30)

like if an array exists in both arrays it gets ‘fused’ instead of having 2 of it in the merged array.

If by

like if an array exists in both arrays it gets ‘fused’ instead of having 2 of it in the merged array.
you mean you want to merge the arrays with no duplicate values, simply use array_unique:

Your code, modified:
[php]$array1 = array( 0 => 10, 1 => 20);
$array2 = array( 0 => 20, 2 => 30);
$merge = array_merge($array1, $array2);
$merge = array_unique($merge);
print_r($merge);[/php]

(I just used array_merge, as the recursive version is pointless in this example - if you are sure you need the recursive version of the function then do of course use it)

Sponsor our Newsletter | Privacy Policy | Terms of Service