Reputation: 747
I'm starting with PHP and I this is the moment when I stuck on an array merging conundrum.
I got 3 arrays:
t1 Array
(
[0] => I
[1] => You
[2] => She
)
t2 Array
(
[0] => am
[1] => are
[2] => is
)
t3 Array
(
[0] => confused
[1] => great
[2] => awesome
)
I would like to merge them in a way which will give me the result as below:
$result = array (
array( 'I', 'am', 'confused' ),
array( 'You', 'are', 'great' ),
array( 'She', 'is', 'awesome' ),
);
The problem is that each array (t1, t2, t3) can accomplish different number of values (the data is taken from uploaded file). For sure every time the numbers of values for t1, t2 and t3 will be equal. I just can't figure it how to do this. Can you please give me a hint?
Upvotes: 0
Views: 79
Reputation: 1
or use a while loop:
$array1 = ["I","You","She"];
$array2 = ["am","are","is"];
$array3 = ["confused","great","awesome"];
$i=0;
while ( $i < 3 ) {
$result[$i] = [$array1[$i],$array2[$i],$array3[$i]];
$i++;
}
Upvotes: 0
Reputation: 212412
Demonstrating SPL's MultipleIterator:
$t1 = array( 'I', 'You', 'She' );
$t2 = array( 'am', 'are', 'is' );
$t3 = array( 'confused', 'great', 'awesome' );
$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($t1));
$mi->attachIterator(new ArrayIterator($t2));
$mi->attachIterator(new ArrayIterator($t3));
$newArray = array();
foreach($mi as $details) {
$newArray[] = $details;
}
var_dump($newArray);
Upvotes: 2