...

View Full Version : Array help



shaileshpatil
05-14-2008, 11:41 AM
Hi

I have 2 arrays of the following structures

$array1[]=array(
'ID' => 1,
'Name' => "SPP"
);
$array1[]=array(
'ID' => 2,
'Name' => "ABC"
);

$array2[]=array(
'ID' => 1,
'Name' => "SPP"
);
$array2[]=array(
'ID' => 13,
'Name' => "ABC"
);


I would like to merge both the arrays which results only details for Id nos 1,2 and 13 to display. How can i achieve this.


Thanks in advance.

shaileshpatil
05-14-2008, 01:56 PM
Any suggestion?

RMcLeod
05-14-2008, 02:17 PM
This Works



<?php
foreach($array1 as $key => $val) {
foreach($array2 as $key2 => $val2) {
if($val['ID'] == $val2['ID']) {
unset($array2[$key2]);
}
}
}

$array3 = array_merge($array1, $array2);

print_r($array3); // Show new array
// Output is
// Array ( [0] => Array ( [ID] => 1 [Name] => SPP ) [1] => Array ( [ID] => 2 [Name] => ABC ) [2] => Array ( [ID] => 13 [Name] => ABC ) )
?>

shaileshpatil
05-14-2008, 04:17 PM
Thanks.
It worked.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum