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条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教