0

I have this object:

stdClass Object
(
[daily_inventoryID] => 1
[inventory_timestamp] => 2012-06-08 14:35:42
[inventory_date] => 2012-06-08
[inventory] =>
Array
(
[0] => stdClass Object
    (
        [ingredientID] => 2
        [code] => Bf
        [description] => 1st Class Flour
        [volume] => 8268
        [price] => 750
        [amount_gram] => 0.02980
        [status] => Inactive
        [uom_id] => 1
        [flour] => Yes
    )

[1] => stdClass Object
    (
        [ingredientID] => 3
        [code] => Sf
        [description] => 3rd Class Flour
        [volume] => 18490
        [price] => 635
        [amount_gram] => 0.02540
        [status] => Inactive
        [uom_id] => 5
        [flour] => Yes
    )
...........

Let's say, we name the objects as $inv, i would like to display the value of $inv->inventory which is an array containing objects.

How would I do that using foreach or for loop?

1 Answer 1

2
foreach ($inv->inventory as $inventory) {
  print_r($inventory);
}
Sign up to request clarification or add additional context in comments.

6 Comments

I got 2 errors : Trying to get property of non-object and invalid argument supplied for foreach()
$inv->inventory is correct. it displays the data when supplying it in the print_r function. but using your code, i still got those errors.
@Jetoox My code is simple enough, if $inv->inventory is an array, how can error happen?
I don't know man. im sure it is an array. the one i posted in my question is the exact form when i tried to print_r $inv->inventory.
@Jetoox Don't tell me that your posted is the result of $inv->inventory, if so, then you need $inv->inventory->inventory.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.