laravel无法显示jsondata

问题描述 投票:0回答:2

我尝试使用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

php json laravel
2个回答
2
投票

好吧,我认为在你的情况下你的阵列中有另一个数组,你需要进入该数组

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

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);
}

请注意所有变化

© www.soinside.com 2019 - 2024. All rights reserved.