Swapping array elements from two different arrays
Swapping array elements from two different arrays
am 10.11.2007 11:28:49 von Chris
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] => c [3] => d )
$array2 - Array ( [0] => a [1] => b [2] => x [3] => x [4] => e [5] =>
f )
Many thanks,
Chris
Re: Swapping array elements from two different arrays
am 10.11.2007 12:18:57 von badar.waqas
On Nov 10, 3:28 pm, Chris 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] => c [3] => d )
>
> $array2 - Array ( [0] => a [1] => b [2] => x [3] => x [4] => e [5] =>
> f )
>
> Many thanks,
>
> Chris
you can use this code
foreach($array1 as $key => $value)
{
if(array_key_exists($key, $array2))
{
$array2[$key] = $value;
}
}
Re: Swapping array elements from two different arrays
am 10.11.2007 15:40:15 von Chris
On 10 Nov, 11:18, "badar.wa...@gmail.com"
wrote:
> On Nov 10, 3:28 pm, Chris 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] => c [3] => d )
>
> > $array2 - Array ( [0] => a [1] => b [2] => x [3] => x [4] => e [5] =>
> > f )
>
> > Many thanks,
>
> > Chris
>
> you can use this code
>
> foreach($array1 as $key => $value)
> {
> if(array_key_exists($key, $array2))
> {
> $array2[$key] = $value;
> }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -
Cheers,
That has solved my problem...fuller snippet given below.
// Performing SQL query
dbConnect("$db");
$query = "SELECT * FROM room";
$result = mysql_query($query) or die("Query failed : " .
mysql_error());
$query_booking = "SELECT * FROM booking WHERE weeknumber =
$weeknumber";
$result_booking = mysql_query($query_booking) or die("Query failed :
" . mysql_error());
while($rowRoom = mysql_fetch_array($result, MYSQL_ASSOC)) {
$timetable = explode(',', $rowRoom['timetable']);
}
while($rowBooking = mysql_fetch_array($result_booking, MYSQL_ASSOC)) {
$period = $rowBooking['period'];
$teacher = $rowBooking['teacher'];
$booking[$period] = "$teacher";
}
foreach($timetable as $key => $value)
{
if(array_key_exists($key, $booking))
{
$int = $key -1;
$timetable[$int] = $booking[$period];
}
}
Chris
Re: Swapping array elements from two different arrays
am 10.11.2007 16:20:37 von luiheidsgoeroe
On Sat, 10 Nov 2007 11:28:49 +0100, Chris =
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:
$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