PHP foreach循环遍历多维数组

Mui*_*ter 2 php foreach for-loop

我有一个多维数组,我该如何使用它?我想在for循环中使用每个单独的数组.

我想要实现的是能够将每个部分放在我的数据库中,就像在db no中输入一样.在db no中0 - > 1和4条目.db - 1中的1 - > 5和6条目.2 - > 1和4以及5和6

我有这个代码:

<?php 
    print_r($calculatie_id); 
    for ($i=0; $i<=3; $i++) 
{ 
?>  
<tr> 
    <td> 

    <?php 
    foreach ($calculatie_id[$i] as $value) 
    { 
        echo $value; 
    } 
?>

print_r($calculatie_id); gives 
Array ( [0] => Array ( [0] => 4 [1] => 6 ) [1] => Array ( [0] => 1 [1] => 5 ) [2] => Array ( [0] => 5 [1] => 6 ) [3] => )
Run Code Online (Sandbox Code Playgroud)

但是在使用时foreach我只得到46

一些额外的信息:

该数组由for循环中的多重选择创建.欢迎任何其他建议.

    for ($i=0; $i<=$aantal_regels_corr; $i++)
{ 
?> 
<tr>
    <td>

        <?php 
        // maak select name
        $calculatie_id = 'calculatie_id'.$i;

        // rest van formulier
        if($error_vergeten[$i] == "ja"){ $error_omschr = $error_omschr_vergeten[$i]; include('includes/input_error.php'); } ?>&nbsp;<textarea rows="7" cols="80" name="omschrijving[]" /><?php echo $omschrijving[$i]; ?></textarea>
    </td>
    <td colspan="2">
        <select multiple="multiple" width="50" size="7" name="<?php echo $calculatie_id ?>[]" style="width: 150px">
        <?php
            $sql_calc = "select id, naam, actief from sp_calculatie WHERE (offerte_id = '".$row['off_offerte']."' OR offerte_id = '".$offerte_id."') AND actief = 'ja' ORDER BY id ASC";
            $res_calc = mysql_query($sql_calc,$con);  
            while ($row_calc = mysql_fetch_assoc($res_calc)){
        ?>
            <option value="<?php echo $row_calc["id"] ?>"><?php if (empty($row_calc["naam"])) { echo $row_calc["id"]; } else { echo $row_calc["naam"]; } ?></option>
        <?php } ?></select>
    </td>
</tr>
<?php } ?>
Run Code Online (Sandbox Code Playgroud)

Cri*_*ian 6

foreach($calculatie_id as $inner_arr)
    foreach($inner_arr as $value)
        echo $value;
Run Code Online (Sandbox Code Playgroud)

编辑:您应该尝试学习并了解这里发生了什么.然后,您可以使用我编写的代码执行任何操作.你说:给14561456我想用$calculatie_id0=14 $calculatie_id1=56 $calculatie_id2=1456等等然后,你将不得不这样做:

foreach($calculatie_id as $index => $inner_arr){
    echo "$calculatie_id$index = "
    foreach($inner_arr as $value)
        echo $value;
    echo "<br/>";
}
Run Code Online (Sandbox Code Playgroud)

或者你可以创建你想要的变量(这对我没有任何意义):

foreach($calculatie_id as $index => $inner_arr){
    ${"calculatie_id$index"} = '';
    foreach($inner_arr as $value)
        ${"calculatie_id$index"} .= $value;
    echo ${"calculatie_id$index"}."<br/>";
}
Run Code Online (Sandbox Code Playgroud)