douzheng0702 2018-05-11 20:49
浏览 54
已采纳

从现有数组创建数组堆栈

Let's say I have an array like this:

$mail[0] = "test1@gmail.com";
$mail[1] = "test2@gmail.com";
$mail[2] = "test3@gmail.com";
$mail[3] = "test4@gmail.com";
$mail[4] = "test5@gmail.com";
$mail[5] = "test6@gmail.com";

Now I want to convert these mails to string and put them in array after every 3 mail of $mail separated by a comma. Something like this -

$email[0] = "test1@gmail.com, test2@gmail.com, test3@gmail.com";
$email[1] = "test4@gmail.com, test5@gmail.com, test6@gmail.com";
$email[2] = "test71@gmail.com, test8@gmail.com, test9@gmail.com";

How can I do this ?

  • 写回答

1条回答 默认 最新

  • duanmo5724 2018-05-11 20:59
    关注

    Slight modification of your code:

    for($i=0; $i<sizeof($oneusers); $i+=3){
        $new[] = $oneusers[$i].", ".$oneusers[$i + 1].", ".$oneusers[$i + 2];
    }
    
    print_r($new);
    

    Going further you can add additional check whether $oneusers[key] exists.

    Another code, which will do the same but without writing i, i+1, i+2:

    $chunks = array_chunk($oneusers, 3);
    foreach ($chunks as $chunk) {
        $new[] = implode(',', $chunk);
    }
    
    print_r($new);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 三菱FX系列PLC串口指令
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型