PHP中数组的For循环
如何将数组中的5个数字分组到每行中?我在下面尝试了这段代码,但结果却出乎我的意料PHP中数组的For循环,php,arrays,for-loop,Php,Arrays,For Loop,如何将数组中的5个数字分组到每行中?我在下面尝试了这段代码,但结果却出乎我的意料 $arrayCount = count($result_data); for ($x = 0; $x < $arrayCount; $x++) { for ($i=0; $i<5; $i++) { echo ($result_data[$i]); } echo ("\n"); }
$arrayCount = count($result_data);
for ($x = 0; $x < $arrayCount; $x++)
{
for ($i=0; $i<5; $i++)
{
echo ($result_data[$i]);
}
echo ("\n");
}
结果:
2392982462468
2392982462468
2392982462468
2392982462468
这个循环不断重复我数组中的前5个数字。我如何使它在我的整个数字数组中每5个数字循环一次?谢谢大家! 对$result\u数据数组了解不多,但可能应该是这样的:
$arrayCount = count($result_data);
for ($x = 0; $x < $arrayCount; $x++)
{
for ($i=0; $i<5; $i++)
{
echo ($result_data[$x][$i]);
}
echo ("\n");
}
使用此$result_数据[$x]
试试这个
$arrayCount = count($result_data);
for ($x = 0; $x < $arrayCount; $x++)
{
if($x%5==0)
{
echo ("\n");
}
echo ($result_data[$x]);
}
$x应该是回音的索引。请尝试以下方法:
<?php
$result_data = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
for ($x = 0; $x < count($result_data); $x++)
{
echo ($result_data[$x]);
if(($x+1)%5==0)
{
echo ("\n");
}
}
我想你想做这样的事
$i = 0;
foreach($result_data as $result) {
echo $result;
if($i < 5) {
echo ",";
} else {
echo "<br/>\n";
$i = 0;
}
$i++;
}
像这样的
$chunks = array_chunk($result_data, 5);
foreach($chunks as $chunk) {
echo implode('', $chunk);
echo "\n";
}
请参见尝试以下几行代码:
$valuesDelimiter = ', ';
$lineDelimiter = "\n";
$input_array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
$slited_array = array_chunk($input_array, 5);
array_walk($slited_array, function(&$arr) {$arr = implode($valuesDelimiter, $arr);});
$result = implode($lineDelimiter, $slited_array);
请显示您的数组和您期望的输出。我想它现在已修复。这将重复相同的字符5次。这是不可能的。您检查过这个吗?我使用的是$x而不是$I。echo语句在我的情况下发生了更改…是的,但是$x在$i循环中没有更改。