如何将3个数组合并为一个大数组(相同的键)

时间:2013-03-05 12:09:51

标签: php arrays

我有3个名为player1,player2,score:

的数组
Array ( [0] => player1 [1] => player1 [2] => player1 ) 
Array ( [0] => player2 [1] => player3 [2] => player2 ) 
Array ( [0] => 2-3 [1] => 1-3 [2] => 2-0 )

我需要的是加入所有这样的数组

[0] => player1, player2, 2-3
[1] => player1, player3, 1-3
[2] => player1, player2, 2-0

我是PHP的新手,我在发布之前使用了搜索栏。请不要投票。

3 个答案:

答案 0 :(得分:1)

试试这个:

array_unshift($array, null);
$res = call_user_func_array('array_map', $array);

echo "<pre>";
print_r($res);

编辑: [10now评论]

       $res = array_map(null, $player1, $player2, $score); 
       echo "<pre>"; 
       print_r($res);

答案 1 :(得分:1)

$array1 = array ( 'player1', 'player1', 'player1' );
$array2 = array ( 'player2', 'player3', 'player2' ) ;
$array3 = array ( '2-3', '1-3', '2-0' );

$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($array1));
$mi->attachIterator(new ArrayIterator($array2));
$mi->attachIterator(new ArrayIterator($array3));

$newArray = array();
foreach ( $mi as $value ) {
    $newArray[] = $value;
    list($team1, $team2, $result) = $value;
    echo $team1 , ' v ' , $team2, ' -> ', $result , '<br />';
}

var_dump($newArray);

答案 2 :(得分:0)

$x=0;
foreach($arr1 as &$e){
    $e.=", $arr2[$x], $arr3[$x]";
    $x++;
}

这应该将第一个数组保留为所需的结果。