0

This is really simple but I need a quick way to do this.

I have three arrays like

$a = array('a','b','c');
$p = array('p','q','r');
$x = array('x','y','z');

How do I combine them to make

array (
    [0] => array ('a','p','x');
    [1] => array ('b','q','y');
    [2] => array ('c','r','z');
);
Doug Neiner
  • 65,509
  • 13
  • 109
  • 118
Atif
  • 10,623
  • 20
  • 63
  • 96

3 Answers3

2
<?php
$a = array('a','b','c');
$p = array('p','q','r');
$x = array('x','y','z');

$arr = array();
for($i=0; $i<count($a); $i++){
  $arr[$i] = array($a[$i], $p[$i], $x[$i]);
}
?>
echo
  • 7,737
  • 2
  • 35
  • 33
2

Wouldn't array_map(null, $a, $p, $x); be better?

See array_map­Docs.

hakre
  • 193,403
  • 52
  • 435
  • 836
Matěj G.
  • 3,036
  • 1
  • 26
  • 27
0

array_map is simpler, but for the sake of possibibility, a quickly typed code example to make use of the MultipleIterator to solve the issue:

$it = new MultipleIterator;
foreach(array($a, $p, $x) as $array) {
    $it->attachIterator(new ArrayIterator($array));
}
$items = iterator_to_array($it, FALSE);

Might be handy in case it's more than an array.

hakre
  • 193,403
  • 52
  • 435
  • 836