我的模型中有以下数组,这里是var_dump:
array(4) {
[0]=>
array(1) {
[0]=>
array(5) {
["payment_student"]=>
string(4) "2406"
["fee_month"]=>
string(10) "2018-02-00"
["arrears"]=>
string(1) "0"
["payment_description"]=>
string(17) "Transport, 120 , "
["payment_amount"]=>
string(3) "120"
}
}
[1]=>
array(1) {
[0]=>
array(5) {
["payment_student"]=>
string(4) "2415"
["fee_month"]=>
string(10) "2018-02-00"
["arrears"]=>
string(1) "0"
["payment_description"]=>
string(33) "Transport, 1500 , Tuition, 620 , "
["payment_amount"]=>
string(4) "2120"
}
}
[2]=>
array(1) {
[0]=>
array(5) {
["payment_student"]=>
string(4) "2175"
["fee_month"]=>
string(10) "2018-02-00"
["arrears"]=>
string(1) "0"
["payment_description"]=>
string(17) "Transport, 620 , "
["payment_amount"]=>
string(3) "620"
}
}
[3]=>
array(1) {
[0]=>
array(5) {
["payment_student"]=>
string(4) "2087"
["fee_month"]=>
string(10) "2018-02-00"
["arrears"]=>
string(1) "0"
["payment_description"]=>
string(33) "Transport, 1500 , Tuition, 420 , "
["payment_amount"]=>
string(4) "1920"
}
}
}
这是我对循环的看法:
$i=0;
foreach ($payments_view as $key => $data) {
?>
<td><?php echo $i; ?></td>
<td><?php echo $data[$i]['payment_student']?></td>
<td><?php echo $data[$i]['arrears']?></td>
<td><?php echo $data[$i]['payment_description']; ?></td>
<td><?php echo $data[$i]['payment_amount']+$data[$i]['arrears']; ?></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php
$i++;
}
显示第1行,但是对于下一行,会出现以下错误:
遇到PHP错误 严重性:注意 消息:未定义的偏移量:1 文件名:views / fee_register_list.php 行号:95 回溯: 文件:C:\ xampp \ htdocs \ learn_ci \ ksi_gentelella \ application \ views \ fee_register_list.php 行:95 功能:_error_handler 文件:C:\ xampp \ htdocs \ learn_ci \ ksi_gentelella \ application \ controllers \ Fee_Controller.php 行:173 功能:查看 文件:C:\ xampp \ htdocs \ learn_ci \ ksi_gentelella \ index.php 行:315 功能:require_once
如何将我的数组简单化为一个数组,以便能够使用它。在这种形状中,我认为它非常复杂。
答案 0 :(得分:1)
试试这个:
for($i=0;$i<count($data);$i++){
<tr>
$firstObj=$data[i][0];
for($j=0;$j<5;$j++){
<td><?php echo $i; ?></td>
<td><?php echo $firstObj[$j]['payment_student']?></td>
<td><?php echo $firstObj[$j]['arrears']?></td>
<td><?php echo $firstObj[$j]['payment_description']; ?></td>
<td><?php echo $firstObj[$j]['payment_amount']." ".$firstObj[$j]['arrears']; ?></td>
}
</tr>
}
答案 1 :(得分:1)
您可以使用array_column
来“扁平化”您的阵列:
<?php foreach (array_column($payments_view, 0) as $i => $data) { ?>
<tr>
<td><?= $i; ?></td>
<td><?= $data['payment_student']?></td>
<td><?= $data['arrears']?></td>
<td><?= $data['payment_description']; ?></td>
<td><?= $data['payment_amount'] + $data['arrears']; ?></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php } ?>
虽然不应该在视野中完成。尝试重构代码,这样您的视图就会以适当的格式显示数据。