Можете ли вы показать мне пример в php о том, как захватить определенную информацию в каком-либо цикле, я хочу получить доступ к [language]
и [answer] => [question] =>
каждый раз, когда он есть. благодаря
Array ( [0] => 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 [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://img.ruphp.com/php/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://img.ruphp.com/php/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 } } }
Вы хотите что-то такое:
<?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"; } } } ?>
Два замечания:
(1) Я предложил использовать var_export () для создания образца для копирования и вставки, чтобы проверить его, но, по-видимому, stdClass :: __ set_state () не может быть легко прочитан . Извините, если я кого-нибудь обманываю.
(2) Это не сложно построить самостоятельно, если вы делаете это шаг за шагом.
<?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. ?>