Reply to Re: Swapping array elements from two different arrays

Your name:

Reply:


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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация