Can you please show me an example in php on how to grab specific information in a loop of some kind, I want to access [language]
and [answer] => [question] =>
everytime it is there. Thanks
Array (
[0] => stdClass Object
(
[type] => text
[cue] => stdClass Object
(
开发者_如何学Python [type] => text
[text] => pri
[sound] =>
[language] => eo
[part_of_speech] => Preposition
)
[responses] => Array
(
[0] => stdClass Object
(
[type] => meaning
[text] => about, concerning
[language] => en
[quizzes] => Array
(
[0] => stdClass Object
(
[type] => multiple_choice
[answer] => pri
[question] => about, concerning
)
)
)
)
[author] => stdClass Object
(
[profile] => stdClass Object
(
[name] => Tomo
[profile_url] => http://smart.fm/users/yubizume
[icon_url] => http://assets2.smart.fm/assets/users/yubizume/e1b0e7f8_medium.jpg
)
[username] => yubizume
)
[language] => eo
[dc_creator] => yubizume
[href] => http://smart.fm/items/830187
[id] => 830187
)
[1] => stdClass Object
(
[type] => text
[cue] => stdClass Object
(
[type] => text
[text] => pri
[sound] =>
[language] => eo
[part_of_speech] => Preposition
)
[responses] => Array
(
[0] => stdClass Object
(
[type] => meaning
[text] => about, concerning (prep)
[sound] => http://assets0.smart.fm/assets/generated_sounds/2009111923/a857356fb588022411fa21371a96e494.mp3
[language] => en
[quizzes] => Array
(
[0] => stdClass Object
(
[type] => multiple_choice
[answer] => pri
[question] => about, concerning (prep)
)
)
)
)
[author] => stdClass Object
(
[profile] => stdClass Object
(
[name] => AriadneAranea
[profile_url] => http://smart.fm/users/AriadneAranea
[icon_url] => http://a1.twimg.com/profile_images/489977042/2009-01_-_New_Year_at_Slimbridge_02_bigger.jpg
)
[username] => AriadneAranea
)
[language] => eo
[dc_creator] => AriadneAranea
[href] => http://smart.fm/items/1773996
[id] => 1773996
)
// $objects is the array with all those objects
foreach($objects as $object)
{
echo $object->cue->language; // language
foreach($object->responses as $response)
{
// if there are no quizzes, we skip the part below
// we skip it because $object->quizzes will produce a warning or a notice
// if "quizzes" is not a present in the $object
if(!isset($object->quizzes))
{
continue;
}
// quizess
foreach($response->quizzes as $quiz)
{
echo $quiz->question; // question
echo $quiz->answer; // answer
}
}
}
You want something lke this:
<?php
foreach($foo as $i){
echo 'Language (cue): ' . $i['cue']['language'] . "\n";
foreach($i['responses'] as $j){
echo 'Language (response): ' . $j['language'] . "\n";
foreach($j['quizzes'] as $k){
echo 'Answer: ' . $k['answer'] . "\n";
echo 'Question: ' . $k['question'] . "\n";
}
}
}
?>
Two remarks:
(1) I suggested using var_export() to generate a copy and paste sample to test with it but apparently stdClass::__set_state() cannot be read back easily. Sorry if I mislead anyone.
(2) It's not difficult to build it yourself if you do it step by step.
<?php
// First
foreach($foo as $c => $v){
var_dump($c, $v);
exit;
}
// Second
foreach($foo as $i){
echo 'Language (cue): ' . $i['cue']['language'] . "\n";
foreach($i['responses'] as $c => $v){
var_dump($c, $v);
exit;
}
}
// Etc.
?>
精彩评论