PHP - Creating new array from other arrays

Hello everyone!

I’m fairly new to php and have come across this hurdle. I have two arrays whose relationship is only that they both hold the same amount of keys. I want to combine the keys to form a new array.

At the moment I have:

First array: Array ( [0] => 38 [1] => 37 [2] => 36 [3] => 35 [4] => 34 [5] => 33 [6] => 32
Second array: Array ( [0] => AA [1] =>CA [2] => ZU [3] => PI [4] => TG [5] => PQ [6] => LK

And I would like my result to be:

Third array: Array ( [0] => 38AA [1] =>37CA [2] => 36ZU [3] => 35PI [4] => 34TG [5] => 33PQ [6] =>32 LK

Any help would be very much appreciated.

Could you set up a string to read the arrays?

So:
$string = array1. array 2

So that you could do: $string[‘1’][‘1’] to pull it up?

I’m not positive this is good code, but it’s an idea. Perhaps someone could develop this more if there isn’t another (easier) option?

Let say your first array is $a, second array is $b, and resulting array is $c. The code would be:
[php]<?php
$c = array();
foreach($a as $key=>$val){
$c[$key] = $a[$key].$b[$key];
}
?>[/php]

Another way (maybe a little more elegant) is to use array_map() function:
[php]<?php
function DoCombine($x, $y) {
return $x.$y;
}

$c = array_map(“DoCombine”, $a, $b);
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service