【发布时间】:2022-01-10 04:36:45
【问题描述】:
我有这些 for 循环来确定连续的数字。到目前为止,我实现的是以字符串形式打印输出。
$arr = [1,2,3,6,11,5,4,8,9,3];
for($start=0; $start<=count($arr); $start++){
for($end=$start+1; $end<=count($arr); $end++){
$total = $arr[$end] - $arr[$start];
if($total == 1){
echo 'Number is '.$arr[$start].','.$arr[$end].'<br/>';
} else {
echo '';
}
$arr[$start++];
}
}
我的目标是将输出添加到数组中。
我尝试使用多维数组但没有输出显示。
$arr = [1,2,3,6,11,5,4,8,9,3];
$arr3 = [];
for($start=0; $start<=count($arr); $start++){
for($end=$start+1; $end<=count($arr); $end++){
$total = $arr[$end] - $arr[$start];
if($total == 1){
$arr2 = array();
$arr2[] = $arr[$start].','.$arr[$end].'';
$arr3[] = $arr2;
} else {
}
$arr[$start++];
}
}
echo '<pre>';
print_r($arr3);
echo '</pre>';
exit;
如果有人可以帮助我,不胜感激。谢谢。
【问题讨论】:
-
您的目标是按各自子数组中的所有连续数字进行收集/分组吗?