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

报告相同问题?