Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “我该怎么做?”;“循环”;通过foreach循环中的JSON数组?_Php_Json_Foreach - Fatal编程技术网

Php “我该怎么做?”;“循环”;通过foreach循环中的JSON数组?

Php “我该怎么做?”;“循环”;通过foreach循环中的JSON数组?,php,json,foreach,Php,Json,Foreach,我有一个问题,我需要在“foreach循环”中循环一个数组。下面的PHP代码现在正在JSON中循环,寻找[“问题”],并正确循环这些内容 但这些问题有不同数量的[“答案”]。此外,这个JSON是动态生成的,所以我不知道以前会有多少问题/答案 所以我需要的是[“答案”]应该在每个问题中循环使用 在这里学习一些专业知识真是太好了 JSON PHP $questions=array(); $i=0; foreach($key=>$items的问题){ $i++; 如果($i>1){$hide='hid

我有一个问题,我需要在“foreach循环”中循环一个数组。下面的PHP代码现在正在JSON中循环,寻找[“问题”],并正确循环这些内容

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

所以我需要的是[“答案”]应该在每个问题中循环使用
  • 在这里学习一些专业知识真是太好了

    JSON

    PHP

    $questions=array();
    $i=0;
    foreach($key=>$items的问题){
    $i++;
    如果($i>1){$hide='hide';}
    $questions[]=”
    “$items->问题。”
    
    • “$items->答案[0]->答案。”
    "; }
    答案使用嵌套循环

    $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;
    }
    
    $tab=0;
    foreach($问题作为$项目){
    $i++;
    $j=0;
    如果($i>1){$hide='hide';}
    $this_q=”
    “$items->问题。”
    
      "; foreach($items->答案为$ans){ $tab++; $j++; $qa=“$i-$j”; $this_q.=“
    • “$ans->答案。”
    • "; } $this_q.=“
    "; $questions[]=$this\u q; }
    如果您了解如何将foreach循环与数组一起使用,您可以对数组进行json_解码并循环。您遇到了一个解析错误:语法错误,意外'{'第5行谢谢你的观察@raam86!意外复制了一个额外的,但我现在删除了它谢谢你的解释,只是做了一个小的调整,效果很好!再次感谢!
    $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;
    }