PHP Array Merge By Date -
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 final array below output |( merge between 2 array 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 ?
try code.
function combo($array1, $array2) { foreach($array1 $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); // view output
updated code
function combo($array1, $array2) { foreach($array1 $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);
Comments
Post a Comment