0

I know this has been asked here: Loop through an array php and I am looking for a very similar solution for ProductName and ProductId but the previous answers are not helping.

Array ( [0] => stdClass Object ( 
[Id] => A048FDBF-67FF-4D28-8F53-7D41DCAC45A9 
[ProductDetail] => Array 
    ( 
        [0] => stdClass Object 
            ( 
                [ProductId] => A5C43424-FFD3-4097-80D4-01E04F759115 
                [ProductName] => Influencer Track 
                [ProductCode] => 
                [ProductType] => Session 
                [ProductDescription] => 
                [SessionCategoryName] => 
                [SessionCategoryId] => 00000000-0000-0000-0000-000000000000 
                [IsIncluded] => 
                [StartTime] => 2019-10-08T08:00:00 
                [EndTime] => 2019-10-08T18:00:00 
                [Status] => Active 
                [Capacity] => -1 
            ) 
        [1] => stdClass Object 
            ( 
                [ProductId] => 163AB381-1C3D-43AE-911D-0729982B8C5C 
                [ProductName] => BS 6:1 How to deal with the new normal using Sales & Operations Planning and Demand Driven MRP 
                [ProductCode] => 
                [ProductType] => Session 
                [ProductDescription] => With the inclusion of Sales and Operations Planning and DDMRP, IFS Applications 10 offers a highly competent set of planning capabilities. Join us in this session to discover how IFS planning strategies can be combined to drive process improvements beyond the expected. 
                [SessionCategoryName] => 
                [SessionCategoryId] => 00000000-0000-0000-0000-000000000000 
                [IsIncluded] => 
                [StartTime] => 2019-10-09T13:00:00 
                [EndTime] => 2019-10-09T13:45:00 
                [Status] => Active 
                [Capacity] => -1 
            ) 
        [2] => stdClass Object 
            ( 
                [ProductId] => 0E1F44BA-54AA-48AC-B54D-0802CCCD3A5A 
                [ProductName] => BS 1:5 Insights session 
                [ProductCode] => [ProductType] => Session 
                [ProductDescription] => [SessionCategoryName] => 
                [SessionCategoryId] => 00000000-0000-0000-0000-000000000000 
                [IsIncluded] => 
                [StartTime] => 2019-10-08T13:45:00 
                [EndTime] => 2019-10-08T14:30:00 
                [Status] => Active 
                [Capacity] => -1 
            ) 
        [3] => stdClass Object 
            ( 
                [ProductId] => DA447217-EA3F-4A14-AD83-0AD184E1D5C4 
                [ProductName] => BS 1:1 Are you ready for tomorrow’s Challengers in Manufacturing? 
                [ProductCode] => 
                [ProductType] => Session 
                [ProductDescription] => Trends like automation, legislation and sustainability present both challenges and opportunities. Our Global Industry Directors will discuss current and emerging trends in the Manufacturing industry and how our customers are addressing them. Join our session to find out how you can leverage the latest trends to disrupt your market. 
                [SessionCategoryName] => 
                [SessionCategoryId] => 00000000-0000-0000-0000-000000000000 
                [IsIncluded] => 
                [StartTime] => 2019-10-08T13:45:00 [EndTime] => 2019-10-08T14:30:00 [Status] => Active [Capacity] => -1 
            ) 
        [4] => stdClass Object 
            ( 
                [ProductId] => 244A8FBB-F731-43B5-909A-0B3DA39DF75C 
                [ProductName] => Influencer Dinner, time tbc 
                [ProductCode] => 
                [ProductType] => Session 
                [ProductDescription] => 
                [SessionCategoryName] => 
                [SessionCategoryId] => 00000000-0000-0000-0000-000000000000 
                [IsIncluded] => 
                [StartTime] => 2019-10-07T19:30:00 
                [EndTime] => 2019-10-07T22:30:00 
                [Status] => Active 
                [Capacity] => -1 
            )
        )
    )
)

I am trying this but I am getting the error Cannot use object of type stdClass as array

for ($i = 0; $i < count($events); $i++) {
      echo $events[$i]['ProductId'];
      echo $events[$i]['ProductName'];
}

Is a forloop the best way to do this and do I have to json_decode or some other method to pull the stdClass Object?

  • Possible duplicate of [Cannot use object of type stdClass as array?](https://stackoverflow.com/questions/6815520/cannot-use-object-of-type-stdclass-as-array) –  Mar 27 '19 at 01:39
  • is the name of the parent array $events – Festus Yuma Mar 27 '19 at 01:49
  • Yeah sorry that wasnt clear, print_r ($events) lists the full array. – fr0styc0d3r Mar 27 '19 at 02:01
  • It looks like $events is your parent array but that is an array itself so in your case $events[0]->ProductDetail is the array you want to iterate over. – Evo_x Mar 27 '19 at 02:37

2 Answers2

0

To access items in an array you use the square brackets $array['ProductId']. But to access property in a object you have to use the arrow notation $object->ProductId. You have a mix of both.

You would need to do something like

echo $events[$i]->ProductId;

From the root to access lets say the first product id you would do this

echo $root[0]->ProductDetail[0]->ProductId;

To access all the product ids you would do something like this

$products = $root[0]->ProductDetail;
for ($i = 0; $i < count($products); $i++) { 
    echo $products[$i]->ProductId; 
}

Or

$products = $root[0]->ProductDetail;
foreach($products as $object) {
    echo $object->ProductId;
}

Both produce same results just 2 ways of doing it. There are other ways. I guess these are most common and a way you have tried. I suggest you use whichever you understand best.

If you want to iterate the outer array then you would need a loop for that too. In that case you would have a nested loop.

In my example i assumed you only traversing the inner product details array.

Mohammad C
  • 1,321
  • 1
  • 8
  • 12
0

try this and see if it works

foreach($events as $event) {
    foreach($event->ProductDetail as $product) {
        echo $product->ProductId;
        echo $product->ProductName;
    }
}
Festus Yuma
  • 155
  • 1
  • 6