I was using this successfully, so I thought:
foreach ($trip->STOPS->STOP as $key=>$value) {
Most of the time the data looks like this:
["STOPS"]=>
object(stdClass)#247 (1) {
["STOP"]=>
array(4) {
[0]=>
object(stdClass)#248 (2) {
["NAME"]=>
string(11) "Short Hills"
["TIME"]=>
string(20) "7/30/2013 6:38:24 AM"
}
[1]=>
object(stdClass)#249 (2) {
["NAME"]=>
string(8) "Millburn"
["TIME"]=>
string(20) "7/30/2013 6:41:24 AM"
}
[2]=>
object(stdClass)#250 (2) {
["NAME"]=>
string(19) "Newark Broad Street"
["TIME"]=>
string(20) "7/30/2013 6:53:00 AM"
}
[3]=>
object(stdClass)#251 (2) {
["NAME"]=>
string(21) "New York Penn Station"
["TIME"]=>
string(20) "7/30/2013 7:13:00 AM"
}
}
}
}
However the above PHP code caused a problem when STOP didn't contain an array of elements, when it looked like this:
["STOPS"]=>
object(stdClass)#286 (1) {
["STOP"]=>
object(stdClass)#287 (2) {
["NAME"]=>
string(21) "New York Penn Station"
["TIME"]=>
string(20) "7/30/2013 8:13:00 AM"
}
}
}
So as you might guess, the instead of it making $key=>$value to be the array element and the array of the NAME/TIME, it was making $key to be NAME or TIME, which is wrong of course.
How can I use this foreach method properly without having to check to see if foreach $trip->STOPS->STOP contains an array or more than one element?
The source of this data is from a SOAP request which is returned as JSON.
Or is my approach here totally wrong? If so, please kindly enlighten me? Thanks!
publicproperties). Besides, if you're more comfortable with an array:$arr = (array) $someObject;is possible (casting objects to array)