Reputation: 73
stdClass Object
(
[request] => stdClass Object
(
[other] => stdClass Object
(
[4] => stdClass Object
(
[answer] => one
)
[5] => stdClass Object
(
[answer] => two
)
[6] => stdClass Object
(
[answer] => three
)
)
)
)
?>
I'm able to print out all the answers by looping with a foreach.
foreach( $result->request->other as $test )
$tests[] = $test->answer;
foreach($tests as $test1){
echo "<p>$test1</p><br>";
}
I'm a little confused on how to also echo out the answer number which in this case is 4 5 6. How do i echo them out as part of the loop. ex:
4 one
5 two
6 three
Upvotes: 0
Views: 39
Reputation: 75645
use foreach( $array AS $key=>$val )
syntax, like this:
foreach( $result->request->other as $key=>$test ) {
printf( "<p>%s: %s</p><br />", $key, $test->answer );
}
Upvotes: 1
Reputation: 6956
foreach( $result->request->other as $key => $test )
$tests[] = $test->answer;
foreach($tests as $test1){
echo "<p>$test1</p><br>";
}
Simply change the foreach line to include a $key => before the $test variable. This will be the index (numeric or associative), and you can get that value and do as you like with it.
Upvotes: 1