1

I have two arrays:

 $a = ['0' => 1, '1' => 2, '2' => 3]
 $b = ['0' => 4, '1' => 5, '2' => 6]

I want to create a new array like this:

[
    ['a' => 1, 'b' => '4'],
    ['a' => '2', 'b' => '5']
]

I have tried using array_merge and array_merge_recursive, but I wasn't able to get the right results.

$data = array_merge_recursive(array_values($urls), array_values($id));
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Energy40
  • 197
  • 1
  • 1
  • 11
  • 1
    I think you have to write your custom function to do what do you want, because you doesn't have any key like 'a' 'b' etc – Sfili_81 Jun 24 '19 at 06:55

5 Answers5

5

You have to apply array_map() with custom function:

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

function combine($n){

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

print_r($newArray);

Output:-https://3v4l.org/okML7

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • 1
    This `array_map(null, $a, $b)` is interesting. Can you explain,please, how it works? – sujeet Jun 24 '19 at 08:50
  • @SujeetAgrahari you can check this link for better explanation:- [Why does array_map() with null as callback create an “array of arrays”?](https://stackoverflow.com/a/31615265/4248328) – Alive to die - Anant Jun 24 '19 at 09:13
0

Try this one

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

It will merge the two array and reduce and remerge it.

Jovs
  • 852
  • 7
  • 23
0

You can use foreach to approach this

$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;
}
Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
0

The idea is to have an array $ab = ['a','b'] and a array from your both arrays like this $merged_array = [[1,4],[2,5],[3,6]].
Now we can combine array $ab with each element of $merged_array and that will be the result we need.

   $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);
sujeet
  • 3,480
  • 3
  • 28
  • 60
0

All earlier answers are working too hard. I see excessive iterations, iterated function calls, and counter variables.

Because there are only two arrays and the keys between the two arrays are identical, a simple foreach loop will suffice. Just push associative arrays into the result array.

Code: (Demo)

$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);

Output:

array (
  0 => 
  array (
    'a' => 1,
    'b' => 4,
  ),
  1 => 
  array (
    'a' => 2,
    'b' => 5,
  ),
  2 => 
  array (
    'a' => 3,
    'b' => 6,
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136