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

时间:2021-08-29 08:37:01

I have 3 arrays called player1, player2, score:

我有3个数组叫做player1,player2,得分:

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

What I need is to join all arrays like this

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

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

I am pretty new to PHP and I used the search bar before posting. Please do not down vote.

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

3 个解决方案

#1


1  

try this :

试试这个 :

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

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

EDIT : [Comment by 10now]

编辑:[10now评论]

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

#2


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);

#3


0  

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

This should leave the 1st array as the desired result.

这应该使第一个数组成为所需的结果。

#1


1  

try this :

试试这个 :

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

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

EDIT : [Comment by 10now]

编辑:[10now评论]

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

#2


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);

#3


0  

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

This should leave the 1st array as the desired result.

这应该使第一个数组成为所需的结果。