在PHP中循环通过stdClass对象
我有一个名为$result的数组,其中包含查询结果。加载视图时,我将变量作为参数传递。我正在尝试访问[文档编号][行编号][描述][类型][数量][度量单位]中的值,并将它们显示给用户 我试图循环遍历数组并不断得到这个错误。无法将stdClass类型的对象用作数组 $result的值 试图访问其中数据的代码 预期结果在PHP中循环通过stdClass对象,php,arrays,Php,Arrays,我有一个名为$result的数组,其中包含查询结果。加载视图时,我将变量作为参数传递。我正在尝试访问[文档编号][行编号][描述][类型][数量][度量单位]中的值,并将它们显示给用户 我试图循环遍历数组并不断得到这个错误。无法将stdClass类型的对象用作数组 $result的值 试图访问其中数据的代码 预期结果 我希望这些值显示在字段上,但我一直在获取此错误消息,无法将stdClass类型的对象用作数组无需将此循环用于每个$line作为$line。您可以直接使用$lines->Descri
我希望这些值显示在字段上,但我一直在获取此错误消息,无法将stdClass类型的对象用作数组无需将此循环用于每个$line作为$line。您可以直接使用$lines->Description您有一个对象数组。每个对象都有可以引用的属性 因此,对于数组$result,可以遍历该数组
foreach($result as $row) {}
然而,每一行都是一个对象;对象属性的引用方式如下:$row->Document\u No
因此,如果您想使用字段[Document_No][Line_No][Description][Type][Quantity]打印表格,可以执行以下操作:
<?php
// do whatever to get $result
// php logic finished...
?>
<table>
<tr>
<th>Document_No</th>
<th>Line_No</th>
<th>Description</th>
<th>Type</th>
<th>Quantity</th>
</tr>
<?php foreach($result as $row): ?>
<tr>
<td><?= $row->Document_No ?></td>
<td><?= $row->Line_No ?></td>
<td><?= $row->Description?></td>
<td><?= $row->Type ?></td>
<td><?= $row->Quantity ?></td>
</tr>
<?php endforeach; ?>
</table>
对于表单,同样的想法也适用于:
<?php foreach($result as $index => $row): ?>
<input disabled id="description" class="form-control input-group-lg reg_name" name=“description[<?= $index ?>]” value=“<?= $row->Description?>” >
...
<?php endforeach; ?>
<?php
// do whatever to get $result
// php logic finished...
?>
<table>
<tr>
<th>Document_No</th>
<th>Line_No</th>
<th>Description</th>
<th>Type</th>
<th>Quantity</th>
</tr>
<?php foreach($result as $row): ?>
<tr>
<td><?= $row->Document_No ?></td>
<td><?= $row->Line_No ?></td>
<td><?= $row->Description?></td>
<td><?= $row->Type ?></td>
<td><?= $row->Quantity ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php foreach($result as $index => $row): ?>
<input disabled id="description" class="form-control input-group-lg reg_name" name=“description[<?= $index ?>]” value=“<?= $row->Description?>” >
...
<?php endforeach; ?>