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');
);
Wouldn't array_map(null, $a, $p, $x);
be better?
See array_map
Docs.
<?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]);
}
?>
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.
精彩评论