0

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.

10now
  • 387
  • 2
  • 3
  • 14
  • 1
    It'd be nice if you included the arrays as they are in your script to make for an easy copy and paste. – Michael Mar 05 '13 at 12:20

3 Answers3

1

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);
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
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);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0
$x=0;
foreach($arr1 as &$e){
    $e.=", $arr2[$x], $arr3[$x]";
    $x++;
}

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

Naryl
  • 1,878
  • 1
  • 10
  • 12