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 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图