在PHP中循环遍历stdClass对象

时间:2019-07-20 12:04:04

标签: php arrays

我有一个名为$ result的数组,其中包含查询结果。加载视图时,我将变量作为参数传递。我正在尝试访问[Document_No] [Line_No] [Description] [Type] [Quantity] [Unit_of_Measure]中的值,并将其显示给用户。

我试图遍历数组并不断收到此错误。不能将stdClass类型的对象用作数组

$ result的值

Array
(
    [0] => stdClass Object
        (
            [Key] => 40;3P4RAwJ7/0kATQBQAC0AMAAwADAAMgAAAACHIE4=7;38242640;
            [Document_No] => IMP-0002
            [Line_No] => 20000
            [Description] => Pay People
            [Type] => TRAVEL
            [Quantity] => 40
            [Unit_of_Measure] => DAY
            [Unit_Price] => 10
            [Amount] => 400
            [Current_Budget] => 2019/2020
            [Account_Type] => G_L_Account
            [Account_No] => 2210301
            [Budgeted_Amount_GoK] => 0
            [Available_Amount] => 37701.41
            [Actual_Spent] => 0
            [Global_Dimension_1_Code_Name] => TUBERCULOSIS
            [Global_Dimension_2_Code_Name] => OTHER PSM COSTS - TB/HIV
            [Global_Dimension_3_Code_Name] => OTHER PSM COSTS
            [Global_Dimension_4_Code_Name] => TB/HIV
            [Global_Dimension_5_Code_Name] => ENGAGING ALL CARE PROVIDERS (MDR-TB)
            [Global_Dimension_6_Code_Name] => NATIONAL TREASURY OF THE REPUBLIC OF KENYA
            [Global_Dimension_1_Code] => TB
            [Global_Dimension_2_Code] => TB13.1.7
            [Global_Dimension_3_Code] => TB7.7
            [Global_Dimension_4_Code] => TBMODT-003
            [Global_Dimension_5_Code] => TBITVT.0010
            [Global_Dimension_6_Code] => TNT
            [Local_Travel] => 
            [International_Travel] => 
        )

    [1] => stdClass Object
        (
            [Key] => 40;3P4RAwJ7/0kATQBQAC0AMAAwADAAMgAAAACHMHU=7;38333880;
            [Document_No] => IMP-0002
            [Line_No] => 30000
            [Description] => Julius Cesear
            [Type] => TRAVEL
            [Quantity] => 8
            [Unit_of_Measure] => DAY
            [Unit_Price] => 70
            [Amount] => 560
            [Current_Budget] => 2019/2020
            [Account_Type] => G_L_Account
            [Account_No] => 2210301
            [Budgeted_Amount_GoK] => 0
            [Available_Amount] => 37701.41
            [Actual_Spent] => 0
            [Global_Dimension_1_Code_Name] => TUBERCULOSIS
            [Global_Dimension_2_Code_Name] => OTHER PSM COSTS - TB/HIV
            [Global_Dimension_3_Code_Name] => OTHER PSM COSTS
            [Global_Dimension_4_Code_Name] => TB/HIV
            [Global_Dimension_5_Code_Name] => ENGAGING ALL CARE PROVIDERS (MDR-TB)
            [Global_Dimension_6_Code_Name] => NATIONAL TREASURY OF THE REPUBLIC OF KENYA
            [Global_Dimension_1_Code] => TB
            [Global_Dimension_2_Code] => TB13.1.7
            [Global_Dimension_3_Code] => TB7.7
            [Global_Dimension_4_Code] => TBMODT-003
            [Global_Dimension_5_Code] => TBITVT.0010
            [Global_Dimension_6_Code] => TNT
            [Local_Travel] => 
            [International_Travel] => 
        )

)

代码尝试访问其中的数据。

<?php  foreach ($result as $lines):
           ?>
            <?php $lineIndex=1;  $count=0; foreach ($lines as $line):

            ?>
      <?php //print_r($lines); exit;?> 
        <div id="lines" class="lines">
            <div id="line" class="line">
                <div class="row">
                <div class="form-group col-lg-2">
                        <input disabled id="description" class="form-control input-group-lg reg_name" type="text"name="quantityinStore[]" value=" <?php if(property_exists($lines[$count], 'Description'))echo $lines[$count]->Description?>" placeholder=""/>
                    </div>

预期结果

我希望这些值出现在字段上,但我不断收到此错误消息无法将stdClass类型的对象用作数组

2 个答案:

答案 0 :(得分:1)

无需使用此循环foreach ($lines as $line)。您可以直接使用$lines->Description

答案 1 :(得分:0)

您有一组对象。每个对象都有可以引用的属性。

因此,对于数组$result,您可以遍历数组。

foreach($result as $row) {}

但是,每一行都是一个对象;像这样引用对象的属性:$row->Document_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; ?>