Foreach loop with mixed stdClass objects and arrays
Question
Here I have an output from a website using Soap
stdClass Object ( [page] => 0 [items] => 3 [total] => 3 [saleItems] => stdClass Object ( [saleItem] => Array ( [0] => stdClass Object ( [reviewState] => open [trackingDate] => 2011-11-03T01:06:43.547+01:00 [modifiedDate] => 2011-11-03T01:06:43.677+01:00 [clickDate] => 2011-10-30T22:57:57.383+01:00 [adspace] => stdClass Object ( [_] => Beslist.nl [id] => 1437603 ) [admedium] => stdClass Object ( [_] => 001. Program logo [id] => 535098 ) [program] => stdClass Object ( [_] => Zavvi NL [id] => 8991 ) [clickId] => 1565847253976339456 [clickInId] => 0 [amount] => 40.45 [commission] => 2.83 [currency] => EUR [gpps] => stdClass Object ( [gpp] => Array ( [0] => stdClass Object ( [_] => shoplink [id] => zpar0 ) ) ) [trackingCategory] => stdClass Object ( [_] => Default [id] => 45181 ) [id] => 46a4f84a-ba9a-45b3-af86-da5f3ec29648 ) ) ) )
I want to have the data (with a foreach loop) from program, commission and gpp->_. I can get the data from program and commission like this:
foreach ($sales->saleItems->saleItem as $sale) {
$programma = $sale->program->_;
$commissie = $sale->commission;
}
Works like a charm. However I can't get the data from the gpp->_
(want to have shoplink as result). I currently have:
foreach ($sales->saleItems->saleItem->gpps->gpp as $tracking) {
echo $tracking->_;
}
I get the error "Trying to get property of non-object". I've tried lots if variations and can't get it to work. Think I'm really close. Anyone has a solution?
Solution
This should work
foreach ($sales->saleItems->saleItem as $sale) {
foreach($sale->gpps->gpp as $tracking) {
echo $tracking->_;
}
As saleItem
is an array, you won't be able to use chaining on it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow