Php Laravel:如何在blade视图中拆分/获取json嵌套元素?
当我通过下面的视图时,blade显示了从mongo获取的整个Json结构Php Laravel:如何在blade视图中拆分/获取json嵌套元素?,php,json,laravel,mongodb,Php,Json,Laravel,Mongodb,当我通过下面的视图时,blade显示了从mongo获取的整个Json结构 {!! $fees !!} 输出: [{"_id":"5e04a06ca445f78401a16d0a","University":[{"fees":"$200"},{"month":"June"}]}] Array ([0] => Array ([_id] => 5e04a06ca445f78401a16d0a [University] => Array ([0] => Array ( [f
{!! $fees !!}
输出:
[{"_id":"5e04a06ca445f78401a16d0a","University":[{"fees":"$200"},{"month":"June"}]}]
Array ([0] => Array ([_id] => 5e04a06ca445f78401a16d0a [University]
=> Array ([0] => Array ( [fees] => $200 ) [1] => Array ( [month] => June ) )))
fees $200month June
现在如何获取嵌套内容。我想在blade中显示Fees=$200和Month=June
像这样尝试,但结果在刀片上为“空白”,没有任何错误。在上面的JSOn输入中,嵌套的open/closing:[,就在'University:'之后,是否存在任何问题。请建议
@foreach ($fees as $value)
{{$content[] = $value->['University'] }}
@foreach ($content as $key)
<h1>{{ $key['fees'] }}</h1>
<h1>{{ $key['month'] }}</h1>
@endforeach
@endforeach
而且
我还从控制器中尝试了以下操作:
..
public function getFees()
{
# database fetch test (with any one of the db query)
$fees = Finance::all();
$fees = Finance::where(['University.month' => 'June'])->get()->toArray();
$fees = Finance::where('University.month', 'June')->first();
$fees = Finance::where('University.month', 'June')->get();
# Return test (with any one of the return)
return view('tables')->with('fees', json_encode($fees, true));
return view('tables', compact('fees'));
return view('tables')->with(compact('fees'));
return view('tables')->with('fees', $fees);
}
..
编辑2:
在视图中,我尝试了如下操作,但得到的例外是:尝试获取非对象的属性“费用”
<?php
$fees = json_decode($fees, true);
#echo "</br>";
#print_r($fees)
?>
@foreach ($fees[0] as $value)
@php $content = $value->University @endphp // or without @
@foreach ($content as $key)
<h1>{{ $key['fees'] }}</h1>
<h1>{{ $key['month'] }}</h1>
@endforeach
@endforeach
唯一的输出是合并,没有逗号分隔。
我可以如下展示它们吗
费用=200美元
月份=六月
或者作为html
<td>{{$k}}</td><td>{{$v}}</td>
其工作原理如下:-
@foreach ($fees as $value)
{{$content[] = $value->['University'] }}
@if($content->['fees']!==null)
<h1>{{ $content['fees'] }}</h1>
@else
<h1>-</h1>
@endif
@if($content->['fees']!==null)
<h1>{{ $content['fees'] }}</h1>
@else
<h1>-</h1>
@endif
@endforeach
您可以尝试以下方法:
$fees = json_decode($fees);
$univ = $fees[0]->University;
//print_r($univ);
foreach ($univ as $key => $value) {
foreach($univ[$key] as $k =>$v){
echo $k .'= '.$v.' ';
}
}
那么,用$value->University而不是“$value->['University']”来做什么呢?你想做什么sense@AlbertoSinigaglia是的,这段代码是错误的。它无法找到带有$value->University的elementstry,因此这应该可以工作。$value['University']您是否尝试过json_decode$fees,对吗?如果它以输出中显示的方式在呈现页面中打印出来:那么它似乎还不是一个PHP数组,而是一个包含json的字符串。这不起作用。有关更多信息,请参阅我的编辑。使用json_decode for$fees=json_decode$fees,对;以及foreach$fees[0]作为$value尝试thisphp$content=$value->University endphp,因为$fees[0]是objectshow试图获取非对象的属性'fees'。我像这样尝试Edit2$fees=json_decode$fees;$univ=$fees[0]->University;//print_r$univ;foreach$univ as$key=>$value{foreach$univ[$key]as$k=>$v{echo$k..$v;}这将起作用。这不起作用。有关详细信息,请参阅我的编辑。
fees $200month June
<td>{{$k}}</td><td>{{$v}}</td>
@foreach ($fees as $value)
{{$content[] = $value->['University'] }}
@if($content->['fees']!==null)
<h1>{{ $content['fees'] }}</h1>
@else
<h1>-</h1>
@endif
@if($content->['fees']!==null)
<h1>{{ $content['fees'] }}</h1>
@else
<h1>-</h1>
@endif
@endforeach
$fees = json_decode($fees);
$univ = $fees[0]->University;
//print_r($univ);
foreach ($univ as $key => $value) {
foreach($univ[$key] as $k =>$v){
echo $k .'= '.$v.' ';
}
}