|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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; } } |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|