PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Swapping array elements from two different arrays
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Swapping array elements from two different arrays

Réponse
 
LinkBack Outils de la discussion
Vieux 10/11/2007, 10h28   #1
Chris
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Swapping array elements from two different arrays

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

  Réponse avec citation
Vieux 10/11/2007, 11h18   #2
badar.waqas@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Swapping array elements from two different arrays

On Nov 10, 3:28 pm, Chris <matchett...@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] => 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;
}
}

  Réponse avec citation
Vieux 10/11/2007, 14h40   #3
Chris
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Swapping array elements from two different arrays

On 10 Nov, 11:18, "badar.wa...@gmail.com" <badar.wa...@gmail.com>
wrote:
> On Nov 10, 3:28 pm, Chris <matchett...@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] => 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

  Réponse avec citation
Vieux 10/11/2007, 15h20   #4
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Swapping array elements from two different arrays

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] => c [3] => d )
>
> $array2 - Array ( [0] => a [1] => b [2] => x [3] => x [4] =>e [5] =>
> f )
>
> Many thanks,


A foreach loop would do. If your actual array has strings for keys, not
integers, another approach is this:

<?php
$array1 = array('a' => 'foo', 'd' => 'bar','y' => 'not valid');
$array2 = array('a' => 'x','b' => 'foz','c' => 'baz','d' => 'x');

$rows_to_insert = array_intersect_key($array1,$array2);
$merged = array_merge($array2,$rows_to_insert);
print_r($merged);
?>
--
Rik Wasmus
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h11.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10476 seconds with 12 queries