|
Posted by Rik Wasmus on 11/10/07 15:20
On Sat, 10 Nov 2007 11:28:49 +0100, Chris <matchett123@googlemail.com> =
wrote:
> How would I replace the elements with matching keys between $array1
> and $array2 to end up with 'a,b,c,d,e,f'.
>
> $array1 - Array ( [2] =3D> c [3] =3D> d )
>
> $array2 - Array ( [0] =3D> a [1] =3D> b [2] =3D> x [3] =3D> x [4] =3D>=
e [5] =3D>
> f )
>
> Many thanks,
A foreach loop would do. If your actual array has strings for keys, not =
=
integers, another approach is this:
<?php
$array1 =3D array('a' =3D> 'foo', 'd' =3D> 'bar','y' =3D> 'not valid');
$array2 =3D array('a' =3D> 'x','b' =3D> 'foz','c' =3D> 'baz','d' =3D> 'x=
');
$rows_to_insert =3D array_intersect_key($array1,$array2);
$merged =3D array_merge($array2,$rows_to_insert);
print_r($merged);
?>
-- =
Rik Wasmus
[Back to original message]
|