php - 如何在 foreach 循环内通过 JSON 数组 "loop"?

标签 php json foreach

我有一个问题,我是否需要循环遍历“foreach-loop”内的数组。下面的 PHP 代码现在循环遍历 JSON 并查找 [“Question”],并正确循环它们。

但是这些问题的[“答案”]数量不同。而且这个 JSON 是动态生成的,所以我之前不知道会有多少问题/答案。

所以我需要的是[“答案”]应该在 <li> 内循环在每个问题中。

如果您能在这里获得一些专业知识,我将非常感激!

JSON

     ["Questions"]=>
      array(2) {
        [0]=>
        object(stdClass)#13 (2) {
          ["Question"]=>
          string(30) "My first questions is this...."
          ["Answers"]=>
          array(2) {
            [0]=>
            object(stdClass)#14 (2) {
              ["Answers"]=>
              string(18) "I like this answer"
              ["Correct"]=>
              bool(true)
            }
          }
        }
        [1]=>
        object(stdClass)#16 (2) {
          ["Question"]=>
          string(22) "Another funny question"
          ["Answers"]=>
          array(2) {
            [0]=>
            object(stdClass)#17 (2) {
              ["Answers"]=>
              string(9) "Or is it?"
              ["Correct"]=>
              bool(false)
            }
            [1]=>
            object(stdClass)#18 (2) {
              ["Answers"]=>
              string(10) "Yes it is!"
              ["Correct"]=>
              bool(true)
            }
          }
        }
      }

PHP

$questions = array();

$i = 0;
foreach($question as $key => $items) {
$i++;

if ($i>1) {$hide=' hide';}
$questions[] = "
            <div class='question-holder" . $hide . "' id='q" . $i . "'>   
                <h2>" . $items->Question . "</h2>
                    <ul class='answers' id='quiz-answers" . $i . "'>
                        <li>
                            <input tabindex='1' type='radio' name='txtAnswer" . $i . "' id='txtAlt1' value='sdsds'>
                            <label for='txtAlt1'><span>" . $items->Answers[0]->Answers . "</span></label>
                        </li>
                    </ul>
            </div>";
} 

最佳答案

使用嵌套循环来获取答案。

$tab = 0;
foreach ($question as $items) {
    $i++;
    $j = 0;
    if ($i > 1) { $hide = ' hide'; }
    $this_q = "<div class='question-holder" . $hide . "' id='q" . $i . "'>   
            <h2>" . $items->Question . "</h2>
                <ul class='answers' id='quiz-answers" . $i . "'>
    ";
    foreach ($items->Answers as $ans) {
        $tab++;
        $j++;
        $qa = "$i-$j";
        $this_q .= "<li>
                        <input tabindex='" . $tab . "' type='radio' name='txtAnswer" . $qa . "' id='txtAlt" . $qa . "' value='sdsds'>
                        <label for='txtAlt" . $qa . "'><span>" . $ans->Answers . "</span></label>
                    </li>
        ";
    }
    $this_q .= "</ul>
        </div>";
    $questions[] = $this_q;
}

关于php - 如何在 foreach 循环内通过 JSON 数组 "loop"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18283181/

相关文章:

powershell - 如何在将两个哈希表与 Powershell 进行比较时选择特定的哈希表键和值?

php - 分页拆分?我忘记了这个词>_<

php - 通过 shell 执行命令后不返回输出

python - 如何摆脱 Flask/mongoengine 的 json 响应中的数据类型

php - 罗马数字转整数函数

vue.js - 如何在foreach(Vuex)中从状态中引用元素?

php - 为什么 Laravel 在插入时运行选择查询

javascript - 将 MySQL 查询结果传递给 JavaScript 函数

javascript - 在 Angular 4 中加载articles.js 库

来自 python 的 javascript 可读的 json