Reputation: 73
I have 2 array , each array has 2 attribute
first array :
Array (
[0] => Array
(
[uregisterDate] => 2013-04-03
[total] => 4
)
[1] => Array
(
[uregisterDate] => 2013-04-04
[total] => 4
)
[2] =>; Array
(
[uregisterDate] => 2013-04-05
[total] => 3
)
)
second array :
Array (
[0] => Array
(
[uregisterDate] => 2013-04-03
[totalFailed] => 2
)
[1] => Array
(
[uregisterDate] => 2013-04-04
[totalFailed] => 4
)
)
I want the final array to be like below output |( merge between two array with key uregistredDate:
Array (
[0] => Array
(
[uregisterDate] => 2013-04-03
[total] => 4
[totalFailed] => 2
)
[1] => Array
(
[uregisterDate] => 2013-04-04
[total] => 4
[totalFailed] => 4
)
[2] =>; Array
(
[uregisterDate] => 2013-04-05
[total] => 3
)
)
any idea , snippet code ?
Upvotes: 4
Views: 2354
Reputation: 1158
try this code.
function combo($array1, $array2) {
foreach($array1 as $key => $value) {
if(isset($array2[$key]))
$result[$key] = array_merge($value, $array2[$key]);
else
$result[$key] = $value;
}
return $result;
}
$array = combo(#your array 1#, #your array 2#);
print_r($array); // to view the output
UPDATED CODE
function combo($array1, $array2) {
foreach($array1 as $key => $value) {
if(isset($array2[$key])) {
if($value['uregisterDate'] == $array2[$key]['uregisterDate'])
{
$result[$key] = array_merge($value, $array2[$key]);
}
else
{
$result[$key] = $array2[$key];
$result[rand(0,99)]= $value;
}
}
else
$result[$key] = $value;
}
return array_values($result);
}
$array = combo($a1, $a2);
print_r($array);
Upvotes: 2
Reputation: 274
Try something like:
function combine( $key, $array1, $array2 )
{
$args = func_get_args();
array_shift( $args );
$return = $args[0];
$arrays = array_slice( $args, 1 );
foreach ( $arrays as $array ) {
foreach ( $array as $arrayValue ) {
$match_found = false;
foreach ( $return as $i => $value ) {
if ( isset( $value[$key] ) && isset( $arrayValue[$key] ) && $value[$key] == $arrayValue[$key] ) {
$return[$i] = array_merge( $value, $arrayValue );
$match_found = true;
}
}
if ( !$match_found ) {
$return[] = $arrayValue;
}
}
}
return $return;
}
$array = combine( "uregisterDate", $array1, $array2 );
Upvotes: 0
Reputation: 4676
Try this function
$array1 = array(
array( "uregisterDate" => '2013-04-03', "total" => 4 ),
array( "uregisterDate" => '2013-04-04', "total" => 4 ),
array( "uregisterDate" => '2013-04-05', "total" => 3 )
);
$array2 = array(
array( "uregisterDate" => '2013-04-03', "totalFailed" => 2),
array( "uregisterDate" => '2013-04-04', "totalFailed" => 3 )
);
function merge_array_common_key($a1, $a2, $Ckey) {
$merge = array_merge($a1,$a2);
$keys = array();
foreach ($merge as $key => $value) {
if(isset($keys[$value[$Ckey]])){
$merge[$keys[$value[$Ckey]]] += $value;
unset($merge[$key]);
continue;
}
$keys[$value[$Ckey]] = $key;
}
return $merge;
}
$test = merge_array_common_key($array1, $array2, 'uregisterDate');
var_dump($test);
Upvotes: 1
Reputation: 7918
I think that the inbuilt function array_merge_recursive() would be gr8:
$array = array_merge_recursive($array1, $array2);
or function may be helpfull
$newarray = Array();
foreach ($arr1 as $element=>$value){
$newarray = array_merge($arr1[$element],$arr2[$element])
}
Upvotes: -1