dongshadu4498 2014-05-23 15:55
浏览 78
已采纳

PHP:通过索引将3个数组合并为一个多维数组

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?

  • 写回答

2条回答 默认 最新

  • doupeng3501 2014-05-23 16:14
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥18 模拟电路问题解答有偿
  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题