FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Joining two arrays?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Joining two arrays? [message #174419 is a reply to message #174415] Fri, 10 June 2011 19:31 Go to previous messageGo to previous message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma:
Senior Member
On 10-06-2011 20:14, Mike wrote:
> How can I combine two 2-dimensional arrays into a single array using a
> matching key value. Here's an example:
>
> I have these two arrays:
>
> $arr1 = array(
> array('key'=>7, 'first_name'=>'Joe'),
> array('key'=>14, 'first_name'=>'Susan')
> );
> $arr2 = array(
> array('key'=>14, 'last_name'=>'Doe'),
> array('key'=>7, 'last_name'=>'Anderson')
> );
>
> I want to create a second array JOINing the two based on the key
> value, giving me:
>
> $resulting_arr = array(
> array('key'=>7, 'first_name'=>'Joe','last_name'=>'Anderson'),
> array('key'=>14, 'first_name'=>'Susan','last_name'=>'Doe')
> );
>
> Thanks!
>
> Mike

<?php
$arr1 = array(
array('key'=>7, 'first_name'=>'Joe'),
array('key'=>14, 'first_name'=>'Susan')
);
$arr2 = array(
array('key'=>14, 'last_name'=>'Doe'),
array('key'=>7, 'last_name'=>'Anderson')
);

//print_r($arr1);
//print_r($arr2);

foreach ($arr1 as $key1 => $row) {
$first_name[$row['key']] = $row['first_name'];
}
foreach ($arr2 as $key1 => $row) {
$last_name[$row['key']] = $row['last_name'];
}

//print_r($first_name);
//print_r($last_name);

foreach ($first_name as $key => $value) {
print $key.": ".$first_name[$key]." ".$last_name[$key]."\n";
}

?>


--
Luuk
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: Setting PDO::ATTR_ERRMODE to E_WARNING before connecting!
Next Topic: haiii
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 21:50:12 GMT 2024

Total time taken to generate the page: 0.04797 seconds