我从PHP开始,这是我遇到数组合并难题的那一刻。
我得到了3个数组:
t1 Array
(
[0] => I
[1] => You
[2] => She
)
t2 Array
(
[0] => am
[1] => are
[2] => is
)
t3 Array
(
[0] => confused
[1] => great
[2] => awesome
)
我想合并他们的方式,将给我的结果如下:
$result = array (
array( 'I', 'am', 'confused' ),
array( 'You', 'are', 'great' ),
array( 'She', 'is', 'awesome' ),
);
问题是每个数组(t1, t2, t3)可以完成不同数量的值(数据取自上传的文件)。可以肯定的是每次t1 t2 t3的值都是相等的。我只是不知道该怎么做。你能给我个提示吗?
演示SPL的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);
或者使用while循环:
$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++;
}