Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 字符串数组元素打印在新行中_Php_Arrays - Fatal编程技术网

Php 字符串数组元素打印在新行中

Php 字符串数组元素打印在新行中,php,arrays,Php,Arrays,这是我的密码: <?php function spilt($number){ $array = array("john doe alex hales johnny johnn arnold ronnie", "john doe alex hales johnny johnn arnold ronnie", "john doe alex hales johnny johnn arnold ronnie");

这是我的密码:

<?php

function spilt($number){
    $array = array("john doe alex hales johnny johnn arnold ronnie",
                   "john doe alex hales johnny johnn arnold ronnie",
                   "john doe alex hales johnny johnn arnold ronnie");

    for ($i=0; $i <count($array) ; $i++) {
        $string =  explode(" ", $array[$i]);
        echo"<br>";
        for ($j = 0; $j < count($string); $j++) {
            echo $string[$j]." ";
            if ($j+1 == $number ) {
                echo "<br>";
            }
        }
    }
}

spilt(3);
  • 当我将2传递给函数时,输出应如下所示:

    john  
    doe 
    alex 
    hales 
    johnny 
    johnn 
    arnold 
    ronnie
    
    john doe 
    alex hales  
    johnny johnn 
    arnold ronnie
    
  • 当我将3传递给函数时,输出应如下所示,以此类推:

    john doe alex
    hales johnny johnn
    arnold ronnie
    

  • 有可能吗?

    你唯一的问题是:
    如果($j+1==$number)

    该条件意味着您仅在
    $j+1
    正好等于
    $number
    时插入新行。您需要查看它是否可以被
    $number
    整除

    if (($j + 1) % $number == 0)
    

    我不明白,你有什么问题?你已经给出了结果和你的代码,但是不应该发生的事情是这样的,反之亦然。只要使用。我的代码运行得很好,我只想当我传递值到函数时,所有数组元素都会根据我传递的值断开。我想要的输出我已经写在上面了。我不知道怎么解决它。代码运行良好我的代码运行良好-那么你就没有问题了,或者你没有在你的OP.bro中适当地解释它。你能根据我想要的输出帮助我吗@谢谢你的回答。我很忙。不客气。