开发者

Joining Arrays in PHP [duplicate]

开发者 https://www.devze.com 2022-12-15 07:32 出处:网络
This question already has answers here: Transposing multidimensional arrays in PHP (12 answers) Closed 8 months ago.
This question already has answers here: Transposing multidimensional arrays in PHP (12 answers) Closed 8 months ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号