简体   繁体   中英

How to merge 3 arrays into one big array (same keys)

I have 3 arrays called 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 )

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.

try this :

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

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

EDIT : [Comment by 10now]

       $res = array_map(null, $player1, $player2, $score); 
       echo "<pre>"; 
       print_r($res);
$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);
$x=0;
foreach($arr1 as &$e){
    $e.=", $arr2[$x], $arr3[$x]";
    $x++;
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM