isset($var) - BLADE 与原始 php

isset($var) - BLADE versus raw php

我正在使用 Laravel 4.2。我有一个情况需要在 laravel blade..

中解决

在模板中我有这个(而且工作正常)

    <?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 

当我尝试对 blade 做同样的事情时

    <?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'

帮忙?

根据您的原始代码和有效的逻辑 - 您的 @foreach 循环应该是这样的

@forreach ($faqGroup as $key => $faq)
    @if (isset($faq[$Q]) && isset($faq[$A])) 
        {{ $faq[$Q] }} <br/>
        {{ $faq[$A] }} <br/><br/>
    @endif
@endforeach