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));
1
  • 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 Commented Jun 24, 2019 at 6:55

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

2 Comments

This array_map(null, $a, $b) is interesting. Can you explain,please, how it works?
@SujeetAgrahari you can check this link for better explanation:- Why does array_map() with null as callback create an “array of arrays”?
1

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,
  ),
)

If we may interpret your requirements to not preserve the numeric keys, then this functional process is pretty slick. Demo

var_export(
    array_map(
        fn(int $a, int $b) => get_defined_vars(),
        $a,  // first array
        $b   // second array
    )
);

Comments

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.

Comments

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;
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.