PHP - Merge 2 multidimensional array based on value -


currently, have 2 multidimensional array , i'm looking combine them 1 giant array value's name in array 1 matches value's name in array 2. array's followed...

array1 (     [0] => array         (             [id] => 1             [name] => test1             [desc] => test_desc             [quantity] => 3         )      [1] => array         (             [id] => 2             [name] => test2             [desc] => test_desc             [quantity] => 33         )  )   array2 (     [0] => array         (             [holder] => 'john'             [name] => test1             [desc] => test_desc             [location] => atl         )      [1] => array         (             [holder] => 'jackie'             [name] => test3             [desc] => test_desc             [location] => sf         )  ) 

i'm looking merge arrays 'name' column in array1 matches in array2 , combine columns in array's 1 & 2 final array. should like...

finalarray (     [0] => array         (             [id] => 1             [holder] => 'john'             [name] => test1             [desc] => test_desc             [location] => atl             [quantity] => 3         )      [1] => array         (             [holder] => 'jackie'             [name] => test3             [desc] => test_desc             [location] => sf         )      [2] => array         (             [id] => 2             [name] => test2             [desc] => test_desc             [quantity] => 33         )  ) 

where "test1" combines different columns across 2 arrays new array inside "finalarray". i've tried researching ideas array_merge , array_merge_recursive i'm not entirely sure if i'm going in correct direction. in advance.

one more solution

function merge_by_name(array $arr1, array $arr2) {     $result = [];      foreach ($arr1 $value) {          $key = array_search($value['name'], array_column($arr2, 'name'));          if($key !== false) {             $result[] = array_merge($value, $arr2[$key]);             unset($arr2[$key]);         } else {             $result[] = $value;         }     }     $result = array_merge($result, $arr2);      return $result; } 

test

$arr1 = [     [         'id' => 1,         'name' => 'test1',         'desc' => 'test_desc',         'quantity' => 3     ],     [         'id' => 2,         'name' => 'test2',         'desc' => 'test_desc',         'quantity' => 33     ], ];  $arr2 = [     [         'holder' => 'john',         'name' => 'test1',         'desc' => 'test_desc',         'location' => 'atl'     ],     [         'holder' => 'jackie',         'name' => 'test3',         'desc' => 'test_desc',         'location' => 'sf'     ], ];  var_export(merge_by_name($arr1, $arr2)); 

result

array (   0 =>    array (     'id' => 1,     'name' => 'test1',     'desc' => 'test_desc',     'quantity' => 3,     'holder' => 'john',     'location' => 'atl',   ),   1 =>    array (     'id' => 2,     'name' => 'test2',     'desc' => 'test_desc',     'quantity' => 33,   ),   2 =>    array (     'holder' => 'jackie',     'name' => 'test3',     'desc' => 'test_desc',     'location' => 'sf',   ), ) 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -