473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

Nov 10 '07 #1
3 2667
On Nov 10, 3:28 pm, Chris <matchett...@googlemail.comwrote:
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;
}
}

Nov 10 '07 #2
On 10 Nov, 11:18, "badar.wa...@gmail.com" <badar.wa...@gmail.com>
wrote:
On Nov 10, 3:28 pm, Chris <matchett...@googlemail.comwrote:
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

Nov 10 '07 #3
On Sat, 10 Nov 2007 11:28:49 +0100, Chris <ma*********@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
Nov 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
10
by: VA | last post by:
I got the following function to swap table columns from somewhere on the Internet function swapColumns (table, colIndex1, colIndex2) { if (table && table.rows && table.insertBefore && colIndex1...
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.