288
36
123
456
789
29
36
123
456
789
295
36
123
456
789
288,29,295是我的第一个数组36,36,36是我的数组数组,123,456,789是我的另一个数组。
我想要显示
288
36
123
29
36
456
295
36
789
这是我的代码
$nos = $_POST['nos'];
$nos2 = $_POST['nos2'];
$nos3 = $_POST['nos3'];
foreach($nos as $nbs)
{
echo $nbs."<br>";
foreach($nos2 as $nbs2)
{
echo " ".$nbs2."<br>";
foreach($nos3 as $nbs3)
{
echo " ".$nbs3."<br>";
}
break;
}
}
我怎么能用foreach做呢 请帮帮我。
答案 0 :(得分:3)
改为使用循环:
$nos = $_POST['nos'];
$nos2 = $_POST['nos2'];
$nos3 = $_POST['nos3'];
for($i = 0 ; $i < count($nos) ; $i++){
if(isset($nos[$i])) echo $nos[$i]."<br>";
if(isset($nos2[$i])) echo " ".$nos2[$i]."<br>";
if(isset($nos3[$i])) echo " ".$nos3[$i]."<br>";
}
或者像这样预先
$nos = $_POST['nos'];
$nos2 = $_POST['nos2'];
$nos3 = $_POST['nos3'];
foreach($nos as $i => $n){
echo $n."<br>";
if(isset($nos2[$i])) echo " ".$nos2[$i]."<br>";
if(isset($nos3[$i])) echo " ".$nos3[$i]."<br>";
}
修改强>
如果您没有数字键或不同的索引,则可以按array_values更改分配http://php.net/manual/en/function.array-values.php
$nos = array_values($_POST['nos']);
$nos2 = array_values($_POST['nos2']);
$nos3 = array_values($_POST['nos3']);
答案 1 :(得分:0)
试试这个, 使用计数器,因此它将显示$ nos3(最后一个foreach循环)值的foreach作为预期输出
$counts = 0;
foreach($nos as $nbs)
{
echo $nbs."<br>";
foreach($nos2 as $nbs2)
{
echo " ".$nbs2."<br>";
$sub_counts = 0;
foreach($nos3 as $nbs3)
{
if($counts == $sub_counts)
{
echo " ".$nbs3."<br>";
}
$sub_counts ++;
}
break;
}
$counts ++;
}
我希望它会有所帮助。
<强> DEMO 强>
答案 2 :(得分:0)
如果您只是调试嵌套数组的内容,并希望以人性化的方式查看它,您可以尝试:
print_r($my_nested_array)
- 请参阅here
或
var_dump($my_nested_array)
- 请参阅here
答案 3 :(得分:-1)
$nos = $_POST['nos'];
$nos2 = $_POST['nos2'];
$nos3 = $_POST['nos3'];
foreach($nos as $nbs) {
echo $nbs . "<br />";
foreach($nos2 as $nbs2) {
echo " " . $nbs2 . "<br />";
foreach($nos3 as $nbs3) {
echo " " . $nbs3 . "<br />";
break; // stop the looping, in this case after the first time
}
break; // stop the looping, in this case after the first time
}
break; // stop the looping, in this case after the first time
}