我使用的是Laravel 4.2。我有一种情况需要在laravel刀片中解决..
在模板中我有这个(它工作正常)
<?php
$faqGroup = json_decode(Lng::faq(), true);
$lang = Lng::faq()['view_as'];
$Q = 'question_' . $lang;
$A = 'answer_' . $lang;
foreach ($faqGroup as $key => $faq) {
if (isset($faq[$Q]) && isset($faq[$A])) {
?>
<?php echo $faq[$Q]; ?> <br/>
<?php echo $faq[$A]; ?> <br/><br/>
<?php
}
}
?>
我的输出是
question1 eng
answer1 eng
question2 eng
answer2 eng
question3 eng
answer3 eng
当我尝试用刀片做同样的事情时
<?php
$faqGroup = json_decode(Lng::faq(), true);
$lang = Lng::faq()['view_as'];
$Q = 'question_' . $lang;
$A = 'answer_' . $lang;
?>
@foreach ($faqGroup as $key => $faq)
{{ isset($faq[$Q]) }} <br/>
{{ isset($faq[$A]) }} <br/><br/>
@endforeach
我的输出是
1
1
1
如果我试试这个
@foreach ($faqGroup as $key => $faq)
{{ $faq->$Q }} <br/>
@endforeach
我得到Trying to get property of non-object
如果我试试这个
@foreach ($faqGroup as $key => $faq)
{{ $faq[$Q] }} <br/>
@endforeach
我得到Illegal string offset 'question_eng'
帮助吗
答案 0 :(得分:1)
根据您正在运行的原始代码和逻辑 - 您的@foreach循环应该是这样的
@forreach ($faqGroup as $key => $faq)
@if (isset($faq[$Q]) && isset($faq[$A]))
{{ $faq[$Q] }} <br/>
{{ $faq[$A] }} <br/><br/>
@endif
@endforeach