laravel无法显示jsondata

时间:2018-11-19 10:04:53

标签: php json laravel

我正在尝试使用jsondata显示结果。
这是我的输出。 现在,我只是尝试显示结果

enter image description here

public function resultsurvey($survey_id)
    {
        //$results=[];

        $allResults= Results::where('survey_id', $survey_id)->get()->toArray(); 
        $justResults=Results::select('results')->where('survey_id', $survey_id)->get();

        $json = json_decode($justResults, true);


        $aQuestions = array();
        $aAnswers = array();
        foreach($json as $sKey => $oItem) {
            array_push($aQuestions, $sKey);
            array_push($aAnswers, $oItem);
        }

        dd($aQuestions , $aAnswers);
}

在纯PHP中,我只使用了一个,但在laravel中却无法使用。

<div class="container"> 
    <table class="table table-striped">
      <thead>
        <tr>
        <?php foreach($aQuestions as $aQuestionsn){?>
          <th scope="col"><?php echo $aQuestionsn; ?></th>
        <?php }?>
        </tr>
      </thead>
      <tbody>
        <tr>
        <?php foreach($aAnswers as $aAnswersn) {?>
            <td><?php echo $aAnswersn;?></td>   
        <?php }?>
        </tr>
      </tbody>
    </table>
</div>  

我如何显示jsonstring?

我所需要的看起来像这样

enter image description here

2 个答案:

答案 0 :(得分:3)

好吧,我认为在您的情况下,您需要在该数组内放入另一个数组

foreach($aAnswers as $aAnswersn)
{
foreach ($aAnswersn as $value)
{
echo $value;
}
}

答案 1 :(得分:0)

至少您的json_encode可能位于错误的位置,请尝试以下操作:

public function resultsurvey($survey_id)
{
    //$results=[];

    $allResults= Results::where('survey_id', $survey_id)->get()->toArray(); 
    $justResults=Results::select('results')->where('survey_id', $survey_id)->get();

    $aQuestions = array();
    $aAnswers = array();
    // $justResults is a collection so loop through it
    foreach($justResults as $sKey => $oItem) {
        // $oItem is possibly JSON encoded at this stage
        $oItem = json_decode($oItem, true);
        array_push($aQuestions, $sKey);
        array_push($aAnswers, $oItem);
    }

    dd($aQuestions , $aAnswers);
}

请注意所有更改