[英]How do you count a multidimensional array to put in a foreach loop (in php)?
如何计算一个foreach循环的数组? 另外,这是编写此代码的最佳方法还是还有其他功能? 我有一个3维数组(3个级别)。
这是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
)
)
)
这是PHP代码:
$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>
我如何计算此for循环的数组?
". 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>";/**/
}
您不能在print
语句中放入其他for
或if
语句。 结束print
语句,然后使用新语句执行所需的操作。
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>";
您可以运行该语句并在以后打印。 我会这样做
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>";
$count=0;
for (x = 0; x< count($value[class_answer]) ; x++){
$count++;
}
print "<br>". $value[repeat_answer] ."<br>";
if ($value[actionID] != 0) {
print $value[action_link];
}
print "</td>
</tr>
</tbody>
</table>
</div>";
$ value [class_answer]中的元素数量为$ count
print $count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.