简体   繁体   English

PHP foreach循环遍历多维数组

[英]PHP foreach loop through multidimensional array

I have an multidimensional array, how can I use it? 我有一个多维数组,我该如何使用它? I want to use each separate array in a for loop. 我想在for循环中使用每个单独的数组。

What I want to achive is to be able to put each part in my database like entry in db no. 我想要实现的是能够将每个部分放在我的数据库中,就像在db no中输入一样。 0 -> 1 and 4 entry in db no. 在db no中0 - > 1和4条目。 1 -> 5 and 6 entry in db no. db - 1中的1 - > 5和6条目。 2 -> 1 and 4 and 5 and 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 foreach I only get 46 但是当使用foreach我只得到46

Some extra information: 一些额外的信息:

This array is created by an multiple select in an for loop. 该数组由for循环中的多重选择创建。 Any other suggestions are welcome. 欢迎任何其他建议。

    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 } ?>
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=1456 etc Then, 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/>";
}

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。

你需要嵌套你的foreach循环(所以你在循环中有一个循环)

You could try: 你可以尝试:

foreach ($calculatie_id as $key => $value) {
  echo "$calculatie_id:".$key;
  for ($i=0;$i<=count($value);$i++) {
    echo $value[$i];
  }
}

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 0:46
1: 15 1:15
2: 56 2:56

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. 我已经尝试了这个剧本超过2周了。 It seems I'm going all the wrong way. 看来我走错了路。

I have put my code online at www.pws.nl/downloads/sp_offerte_add.rar. 我已将我的代码放在www.pws.nl/downloads/sp_offerte_add.rar上。 It must be easier then what I'm writing in the code right now. 它必须比我现在在代码中写的更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM