Kinyua James
Kinyua James

Reputation: 55

Looping Through a stdClass Object in PHP

I have a array called $result that contains the query results. When Loading the view, I'm passing the varible as a parameter. I'm trying to acess the values in [Document_No] [Line_No][Description] [Type] [Quantity] [Unit_of_Measure] and display them to the user.

I have tried to loop through the array and keep getting this error. Cannot use object of type stdClass as array

Values of $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] => 
        )

)

Code trying to Acess the data in it.

<?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>

Expected Results

I want the values to appear on the fields but i keep geting this error message Cannot use object of type stdClass as array

Upvotes: 0

Views: 391

Answers (2)

Tim Morton
Tim Morton

Reputation: 2644

You have an array of objects. Each object has properties that can be referenced.

So, for the array $result, you can iterate through the array.

foreach($result as $row) {}

Each row, however, is an object; properties of an object are referenced like so: $row->Document_No

So if you were wanting to print out a table using fields [Document_No] [Line_No][Description] [Type] [Quantity], you could do this:

<?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>

For a form, the same idea applies:

<?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; ?>

Upvotes: 0

Nilesh Patil
Nilesh Patil

Reputation: 138

No need to use this loop foreach ($lines as $line). You can use directly $lines->Description

Upvotes: 1

Related Questions