PHP foreach 循环遍历多维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2783812/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP foreach loop through multidimensional array
提问by Muiter
I have an multidimensional array, how can I use it? I want to use each separate array in a forloop.
我有一个多维数组,我该如何使用它?我想在for循环中使用每个单独的数组。
What I want to achive is to be able to put each part in my database like entry in db no. 0 -> 1 and 4 entry in db no. 1 -> 5 and 6 entry in db no. 2 -> 1 and 4 and 5 and 6
我想要实现的是能够将每个部分放入我的数据库中,就像 db 中的条目一样。0 -> 数据库编号中的 1 和 4 条目。1 -> 数据库编号中的 5 和 6 条目。2 -> 1 和 4 和 5 和 6
I have this code:
我有这个代码:
<?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] => )
But when using the foreachI only get 46
但是当使用foreach我只得到 46
Some extra information:
一些额外的信息:
This array is created by an multiple select in an for loop. Any other suggestions are welcome.
该数组由 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'); } ?> <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 } ?>
回答by Cristian
foreach($calculatie_id as $inner_arr)
    foreach($inner_arr as $value)
        echo $value;
Edit:you should try to learn and understand what's going on here. Then, you can do whatever you want with the code I wrote. You said: gives 14561456 I want to use $calculatie_id0=14 $calculatie_id1=56 $calculatie_id2=1456etcThen, you will have to do something like this:
编辑:你应该尝试学习和理解这里发生了什么。然后,你可以用我写的代码做任何你想做的事情。你说:给出 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/>";
}
Or you can create those variables you want (which does not make any sense for me):
或者你可以创建你想要的那些变量(这对我来说没有任何意义):
foreach($calculatie_id as $index => $inner_arr){
    ${"calculatie_id$index"} = '';
    foreach($inner_arr as $value)
        ${"calculatie_id$index"} .= $value;
    echo ${"calculatie_id$index"}."<br/>";
}
回答by Daniel
Basically what you have is an array like this:
基本上你有一个这样的数组:
array(
 array(
  data
 )
)
All you need is:
所有你需要的是:
foreach($yourArray as $innerArray)
 foreach($innerArray as $value)
  echo $value;
That will output what you want. I'm not particularly sure why you're doing a for() loop then a foreach.
这将输出你想要的。我不是特别确定您为什么要先执行 for() 循环然后再执行 foreach。
回答by Mitch Dempsey
You need to nest your foreachloops (so you have a loop inside a loop)
你需要嵌套你的foreach循环(所以你有一个循环内的循环)
回答by thesunneversets
You could try:
你可以试试:
foreach ($calculatie_id as $key => $value) {
  echo "$calculatie_id:".$key;
  for ($i=0;$i<=count($value);$i++) {
    echo $value[$i];
  }
}
回答by Kip
I don't understand why your original code snippet isn't working. Could it be the fact that $calculatie_id[3]is not an array?  
我不明白为什么您的原始代码片段不起作用。难道$calculatie_id[3]不是数组的事实吗?  
I tried this:
我试过这个:
<?php
$calculatie_id = array ( array(4,6), array(1,5), array(5,6), '' );
for ($i=0; $i<=3; $i++) 
{ 
  if(!is_array($calculatie_id[$i]))
    continue;
  echo "$i: ";
  foreach ($calculatie_id[$i] as $value) 
  { 
    echo $value; 
  } 
  echo '<br/>';
}
And that prints:
然后打印:
0: 46
1: 15
2: 56
0:46
1:15
2:56
回答by Muiter
Many thanks for helping me out this far but all the examples I try to implement are not working. I have been trying this script for over 2 weeks now. It seems I'm going all the wrong way.
非常感谢您帮助我走到这一步,但我尝试实现的所有示例都不起作用。我已经尝试这个脚本超过 2 周了。看来我走错了路。
I have put my code online at www.pws.nl/downloads/sp_offerte_add.rar. It must be easier then what I'm writing in the code right now.
我已经把我的代码放到了 www.pws.nl/downloads/sp_offerte_add.rar。它一定比我现在在代码中写的更容易。

