Merge column values from two arrays to form an indexed array of associative arrays



PHP Snippet 1:

$newArray = array_map('combine',array_map(null, $a, $b));

function combine($n){

    return array_combine(array('a','b'),$n);
}

print_r($newArray);

PHP Snippet 2:

$c = array_merge($a,$b)
$d[] = array_reduce($d, 'array_merge', []);

PHP Snippet 3:

$a = ['0' => 1, '1' => 2, '2' => 3];
$b = ['0' => 4, '1' => 5, '2' => 6];
$res = [];
$i = 0;
$total = 2;
foreach($a as $k => $v){
  $res[$i]['a'] = $v;
  $res[$i]['b'] = $b[$k];
  $i++;
  if($i == $total) break;
}

PHP Snippet 4:

   $first = ['0' => 1, '1' => 2, '2' => 3];
   $second = ['0' => 4, '1' => 5, '2' => 6];

   $merged_array = [];
   for($i=0;$i<count($first);$i++)
   {
        array_push($merged_array,[$first[$i],$second[$i]]);
   }

   $final = [];
   $ab = ['a','b'];
   foreach($merged_array as $arr)
   {
       array_push($final,array_combine($ab, $arr));
    }
    print_r($final);

PHP Snippet 5:

$a = ['0' => 1, '1' => 2, '2' => 3];
$b = ['0' => 4, '1' => 5, '2' => 6];
$result = [];
foreach ($a as $k => $v) {
    $result[] = ['a' => $v, 'b' => $b[$k]];
}
var_export($result);

PHP Snippet 6:

array (
  0 => 
  array (
    'a' => 1,
    'b' => 4,
  ),
  1 => 
  array (
    'a' => 2,
    'b' => 5,
  ),
  2 => 
  array (
    'a' => 3,
    'b' => 6,
  ),
)