0
stdClass Object
(
    [request] => stdClass Object
        (
            [other] => stdClass Object
                (
                    [4] => stdClass Object
                        (
                            [answer] => one
                        )

                    [5] => stdClass Object
                        (
                            [answer] => two
                        )

                    [6] => stdClass Object
                        (
                            [answer] => three
                        )
                )

        )
)
?>

我可以通过使用 foreach 循环打印出所有答案。

foreach( $result->request->other as $test )
      $tests[] = $test->answer;
            foreach($tests as $test1){
            echo "<p>$test1</p><br>";
          }

我对如何回显答案编号有点困惑,在这种情况下是 4 5 6。我如何将它们作为循环的一部分回显。前任:

4 one
5 two
6 three
4

2 回答 2

1
foreach( $result->request->other as $key => $test )
      $tests[] = $test->answer;
            foreach($tests as $test1){
            echo "<p>$test1</p><br>";
          }

只需将 foreach 行更改为在 $test 变量之前包含 $key => 即可。这将是索引(数字或关联),您可以获得该值并随心所欲地使用它。

于 2012-09-27T23:22:21.687 回答
1

使用foreach( $array AS $key=>$val )语法,像这样:

foreach( $result->request->other as $key=>$test ) {
  printf( "<p>%s: %s</p><br />", $key, $test->answer );
}
于 2012-09-27T23:23:35.780 回答