2

Quick example:

$array_1 = [1, 2, 3];
$array_2 = ['a', 'b', 'c'];
$array_3 = ['white', 'red', 'blue'];

I need an array like:

$array_4 = [
    [1, 'a', 'white'],
    [2, 'b', 'red'],
    [3, 'c', 'blue']
]
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
marin
  • 21
  • 1
  • 2
  • Will the arrays always be of equal length? – sberry Aug 15 '10 at 18:52
  • 2
    @Aillyn I removed the "zip" tag. That's for zip files. Different languages call this a different thing. It could be seen as a transposition or, perhaps more accurately, a generalized inner product. – Artefacto Aug 15 '10 at 19:07
  • @Artefacto It was more of a test of my new retag button than anything else. It's been a while since you've got to 500 I guess... – Aillyn Aug 15 '10 at 19:11
  • @Ailyn You shouldn't use sock-puppet accounts (pessimopoppotamus). I hope at least you're using it to upvote yourself... – Artefacto Aug 15 '10 at 19:14
  • @Artefacto It doesn't say anywhere that I can't have more than one account. You can take it to the admins if you want. I never upvoted myself. – Aillyn Aug 15 '10 at 19:20
  • @Ailyn You're right, it's sort of permitted as long as you're not upvoting yourself (or upvoting someone else more than once). Sorry for the inconvenience. :p – Artefacto Aug 15 '10 at 19:26
  • See http://stackoverflow.com/questions/1860490/interleaving-multiple-arrays-into-a-single-array – nicomen Aug 15 '10 at 18:52

5 Answers5

7

You mean something like Python's zip()? This will do:

$zipped = array_map(null, $array_1, $array_2, $array_3);

If you want a function that can do it with an arbitrary number of arrays, see: Is there a PHP function like Python's zip?

Community
  • 1
  • 1
Aillyn
  • 23,354
  • 24
  • 59
  • 84
2

I guess you are looking for a one-liner? I can't provide that, but this is my attempt:

$array_4=array();
for($i = 0 ; $i<count($array_1) ; ++$i) {
  $array_4[$i] = array($array_1[$i], $array_2[$i], $array_3[$i]);
}

This code of course assumes that all input arrays have the same lengths.

If you have more than 3 input arrays I would put them all in an array and foreach over it:

$all_arrays = array($aray_1, $array_2, ...);
$output_array=array();
for($i = 0 ; $i<count($array_1) ; ++$i) {
  $output_array[$i] = array();
  foreach($all_arrays as $input_array) {
    $output_array[$i][] = $input_array[$i];
  }
}
silvo
  • 4,011
  • 22
  • 26
  • The prefix `++` is evil and apparently confusing you, silvo. Array indices start at `0`. – You Aug 15 '10 at 18:57
  • 3
    @You: In c++/C# it does not matter if pre or post increment is used in the for loop. Do you want to say that it matters in php? I think you are mistaken. – silvo Aug 15 '10 at 19:00
  • @Aillyn: Give me a chance to upskill myself. What exactly is incorrect there? – silvo Aug 15 '10 at 19:02
  • @silvo Your previous answer didn't work. This one still has problems. Try and use it with the OP's inputs. I removed my downvote though. – Aillyn Aug 15 '10 at 19:05
  • @Aillyn: Spotted the missing $s. Thanks. – silvo Aug 15 '10 at 19:25
  • @You - preincrement is good. It is slightly faster than post increment in PHP. In this case using either would give the same result. – Peter Ajtai Aug 16 '10 at 09:20
0

You question is not very clear but:

If you want to combine all the values of 3 or more arrays into one array you can do this:

$array_4 = array_merge($array_1, $array_2, $array_3);

If you want to combine all those arrays into one you can do this:

$array_4[] = $array_1;
$array_4[] = $array_2;
$array_5[] = $array_3;

But I think you are really after for what Silvo posted above, hence +1 to him.

spreadzz
  • 270
  • 3
  • 10
-1

Just a simple array_merge would do this.

Kevin Davis
  • 367
  • 1
  • 7
  • 25
-3

I think the easiest way, without using a bunch of array_X functions would be to simply "concatenate" them all into one array.

For instance

$arr1 = array(1,2,3);
$arr2 = array(a,b,c);
$arr3 = array("red","white","blue");
$bigArray[] = $arr1;
$bigArray[] = $arr2;
$bigArray[] = $arr3;

Then you would have 1 Array containing three separate arrays, assuming that is what you're after.

$bigArray would look something like this

Array (
        0 Array (
           0 => 1
           1 => 2
           2 => 3
       )
        1 Array (
           0 => a
           1 => b
           2 => c
       )
        2 Array (
           0 => red
           1 => white
           2 => blue
       )
)
dockeryZ
  • 3,981
  • 1
  • 20
  • 28