tags:

views:

66

answers:

2

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');
);
+2  A: 
<?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
I was doing the same lol. i thought tehre might be a better way :)
atif089
there's probably a tricky PHPism that would do it, but whatever... get 'r done, as it were.
echo
Better use the `min(count($a), count($p), count($x))` to get the largest common index if the arrays do not have the same size.
Gumbo
arays are always going to be of same size
atif089
+1  A: 

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

Matěj Grabovský