我正在尝试使用jsondata显示结果。
这是我的输出。
现在,我只是尝试显示结果
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?
我所需要的看起来像这样
答案 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);
}
请注意所有更改