dongxi1320 2016-12-14 20:32
浏览 80
已采纳

如何按此顺序打印数组?

Hello guys please help I have array contains three arrays , each array contains a different amount of chars how ever the first array always have the greater amount of chars then the second and so on . Notice that two Consecutive arrays may have the same amount of chars check this example

array     //called lines
(
 ['a','b','c','d'],
 ['e','f','g'],
 ['h','i']
);

Am trying to iterate the main array and each time print the first letter of each array and then shift it out of the array so first time I will print 'a' from the first array shift it from the array and then move to second array and print 'e' and shift it from the array , move to the third and print 'h' the shift it then go back and do the same till all of them are empty so how can I do that here is what the output should look like "aehbficgd" sorry if the question is too long thanks

        while(!empty($lines))
        {
            $counter = 0;

            echo $lines[$counter][0];
            array_shift($lines[$counter]);
            $counter++;

            if($counter == count($lines))
            {
                $counter = 0;
            }
        }
  • 写回答

2条回答 默认 最新

  • doumou1864 2016-12-14 20:39
    关注

    What about this?

    $lines = array
    (
       ['a','b','c','d'],
       ['e','f','g'],
       ['h','i']
    );
    
    $continue = true;
    while($continue)
    {
        $continue = false;
        foreach($lines as $key => $line) {
            if(!empty($line)) {
                echo array_shift($line);
    
                $lines[$key] = $line;
                $continue = true;
            }
        }
    }
    

    You can try it here https://3v4l.org/TrcmA

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?