php 如何通过 Laravel 刀片在阵列内打印阵列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31111991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:15:16  来源:igfitidea点击:

How to print arrays inside array by Laravel blade?

phparrayslaraveltreeviewblade

提问by narsis fe

My function returns an array to a variable in laravel blade page like this :

我的函数将数组返回到 laravel 刀片页面中的变量,如下所示:

Array
(
    [id] => 1
    [name] => Enterpise
    [children] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [name] => name1
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 5
                                    [name] => name2
                                    [children] => 
                                )

                            [1] => Array
                                (
                                    [id] => 6
                                    [name] => name3
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 11
                                                    [name] => name4
                                                    [children] => 
                                                )

                                                               .....

Now I want to print these values inside my page by using something like foreach. Consider that their children's numbers are not specified, it is impossible to do that inside of my function. It is important to me to be done in blade. How can I do that?

现在我想通过使用类似的东西在我的页面中打印这些值foreach。考虑到没有指定他们孩子的号码,在我的函数内部不可能做到这一点。在刀片中完成对我来说很重要。我怎样才能做到这一点?

回答by xxxbence

In your blade files you can use any php functions like print_r().

在您的刀片文件中,您可以使用任何 php 函数,例如print_r().

Example:

例子:

{{ print_r($array) }}

回答by Tushar Vaghela

As this is very old issue but I found jsondirective very useful.

由于这是一个非常古老的问题,但我发现json指令非常有用。

<pre>
    <code>
        @json($array);
    </code>
</pre>

回答by spsaravananct

You can use PHP directive

您可以使用 PHP 指令

@php
 echo "<pre>";
   print_r($array);
 echo "</pre>";
@endphp

回答by ba1ar

You can find the way for your problem over here:

您可以在这里找到解决问题的方法:

https://laracasts.com/discuss/channels/general-discussion/passing-arrays-to-blade-and-iterating

https://laracasts.com/discuss/channels/general-discussion/passing-arrays-to-blade-and-iterating

Or you can use: {{ echo json_encode($yourarray) }}

或者你可以使用:{{ echo json_encode($yourarray) }}

but this will print your array as json encoded string.

但这会将您的数组打印为 json 编码的字符串。