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 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作