dongluo1853 2016-08-15 19:20
浏览 40
已采纳

你如何计算一个多维数组放入foreach循环(在PHP中)?

How do you count an array for a foreach loop? Also, is this the best way to write this code or is there more of a catchall? I have a 3 dimensional array(3 levels).

Here is an example from print_r

Array
(
[0] => Array
   (
        [0] => 17
        [audit_inspectionID] => 17
        [1] => 2016-08-15
        [created] => 2016-08-15
        [2] => 2016-08-15 09:52:28
        [modified] => 2016-08-15 09:52:28
        [class_answer] => Array
            (
                [0] => Needs Improvement     
                [1] => Need To Correct     
                [2] => Needs Immediate Action     
            )
   )

)

Here is the PHP code:

$newArray = [];
foreach($requirements as $key => $value){
    $newArray[] = $value['audit_inspectionID'];
    $newArray[] = $value['requirement'];
    $newArray[] = $value['class_answer'];
    $newArray[] = $value['repeat_answer'];
    $newArray[] = $value['class_answer'];
    $newArray[] = $value['actionID'];
    $newArray[] = $value['action_link'];

    print "<div id='inspection_view" . $value['audit_inspectionID'] . "' style='display:inline'>
        <table id='actions_table' class='table table-bordered table-condensed table-striped bg-info'>
          <thead>
              <th align='center'>" . value['requirement'] . " </th>
              ". if ($corporate_admin == 'true') { ."
                <a id='" . $value['audit_inspectionID'] . "' class='btn btn-danger  pull-right remove1' href=' + '#' + '>Remove</a><a id='edit" . $value['audit_inspectionID'] ."' class='btn btn-warning pull-right edit1' href=#>Edit</a></th>
              ". } ."
              </thead><tbody>
              <tr>
                  <td>

How can I count this array for this for loop?

                 ". for (x = 0; x< count($value[class_answer]) ; x++){ 
                      $value[class_answer] 
                  } ."
                  <br>
                  ". $value[repeat_answer] ."
                  <br>
                  ". if ($value[actionID] != 0) { 
                    $value[action_link]
                  } ."
                  </td>
              </tr>
            </tbody>
        </table>
        </div>";/**/
}
  • 写回答

2条回答 默认 最新

  • doucan9079 2016-08-15 19:38
    关注

    You can't put a other statements like for or if inside a print statement. End the print statement, then use a new statement to do what you want.

    print "<div id='inspection_view" . $value['audit_inspectionID'] . "' style='display:inline'>
        <table id='actions_table' class='table table-bordered table-condensed table-striped bg-info'>
              <th align='center'>" . value['requirement'] . " </th>
              <tr>
                  <td>";
    foreach ($value['class_answer'] as $ans) {
        print "$ans ";
    }
    print "<br>
          ".$value[repeat_answer]."
          <br>";
    if ($value['actionID'] != 0) {
        print $value['action_link'];
    }
    print "</td>
              </tr>
            </tbody>
        </table>
        </div>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么