Array search and combine in PHP -
i have 2 arrays. possible create output array , add keys/values first second one? arrays looks like:
the first:
array ( [0] => array ( [id] => 11 [expire] => undefined ) [1] => array ( [id] => 12 [expire] => undefined ) [2] => array ( [id] => 6 [expire] => 8 ) [3] => array ( [id] => 10 [expire] => 4 ) ) the second:
array ( [0] => array ( [id] => 6 [realname] => to_es.gif [extension] => gif [filesize] => 57885 ) [1] => array ( [id] => 10 [realname] => to_joomla_2_customer_view.gif [extension] => gif [filesize] => 77182 ) [2] => array ( [id] => 11 [realname] => to_nl.gif [extension] => gif [filesize] => 10990 ) [3] => array ( [id] => 12 [realname] => to_pl_1.gif [extension] => gif [filesize] => 52826 ) ) how i'm getting output array:
array ( [0] => array ( [id] => 6 [realname] => to_es.gif [extension] => gif [filesize] => 57885 [expire] => 8 ) [1] => array ( [id] => 10 [realname] => to_joomla_2_customer_view.gif [extension] => gif [filesize] => 77182 [expire] => 4 ) [2] => array ( [id] => 11 [realname] => to_nl.gif [extension] => gif [filesize] => 10990 [expire] => undefined ) [3] => array ( [id] => 12 [realname] => to_pl_1.gif [extension] => gif [filesize] => 52826 [expire] => undefined ) )
try this:
function my_array_merge($first,$second) { $new = array(); foreach ($first $f_item) { foreach ($second $i => $s_item) { if ($f_item['id']===$s_item['id']) { $new[] = $f_item + $s_item; unset($second[$i]); break; } } } usort($new, function($a,$b) { return $a['id'] - $b['id']; }); return $new; } example on ideone: http://ideone.com/tqiikh
Comments
Post a Comment