How to print arrays inside array by Laravel blade?
Asked Answered
P

6

12

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

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?

Presumably answered 29/6, 2015 at 9:19 Comment(0)
H
20

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

Example:

{{ print_r($array) }}
Headcheese answered 28/10, 2016 at 16:39 Comment(1)
dd() is generic function provided by laravel you can use just like native php function in blade. {{ dd($array) }} laravel-news.com/laravel-5-dd-gets-upgrade.Marquess
T
6

As this is very old issue but I found json directive very useful.

<pre>
    <code>
        @json($array);
    </code>
</pre>
Truthful answered 1/8, 2018 at 14:10 Comment(0)
A
0

You can find the way for your problem over here:

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

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

but this will print your array as json encoded string.

Ancient answered 24/8, 2015 at 7:29 Comment(0)
P
0

You can use PHP directive

@php
 echo "<pre>";
   print_r($array);
 echo "</pre>";
@endphp
Prorogue answered 16/10, 2019 at 11:38 Comment(0)
N
0

I always use this clean and simple solution that outputs your array in a nicely formatted way, using JSON:

<pre><code>{{ json_encode($array, JSON_PRETTY_PRINT) }}</code></pre>

You can drop the JSON_PRETTY_PRINT if you want it displayed on a single line:

<pre><code>{{ json_encode($array) }}</code></pre>
Nephrotomy answered 4/12, 2021 at 1:42 Comment(0)
T
0

the best output and less code has print_r() :

<pre>
    {{print_r($array)}}
</pre>
Trela answered 21/1, 2022 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.