Looping through array contain 3 classes
I am having trouble getting my head around how to loop through stdClasses.
Printing the array gives me the following:
Array
(
[piggyback] => Array
(
[0] => stdClass Object
(
[id] => 1003
[entity_id] => 0
[redirect_url] => http://yahoo.com
[type] => Image
)
)
[total_count] => 1
)
Array
(
[piggyback] => Array
(
[0] => stdClass Object
(
[id] => 1002
[entity_id] => 0
[redirect_url] => http://google.com
[type] => Image
)
)
[total_count] => 1
)
Array
(
[piggyback] => Arra开发者_如何学Cy
(
[0] => stdClass Object
(
[id] => 1001
[entity_id] => 0
[redirect_url] => http://bing.com
[type] => Image
)
)
[total_count] => 1
I am trying to loop though with the following and print out a value (id) but I keep getting nothing.
foreach ($piggies_array as $key => $value) {
echo $piggies_array[$key]['id'];
}
foreach ($piggies_array as $key => $value) {
if (is_array($value)){
echo $value[0]->id;
}
}
I think you need:
for ($i = 0; $i < count($piggies_array); $i++) {
echo $piggies_array[$i]['piggyback'][0]->id;
}
...assuming we can only see part of your output ;)
try replace it to echo $value->id;
精彩评论