Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel print array in blade php

I want to show an array in my .blade.php, but it does not work properly so my controller looks like this:

class WatchController extends Controller
{

    public function index()
    {
        $watchFolderPath = 'C:\\xampp\\htdocs\\Pro\\rec\\';
        $watchFolder     = $this->dirToArray($watchFolderPath);
        return view('watch.new')->with('watchFolder', $watchFolder);
    }

    # Get Directories of Path as Array
    function dirToArray($dir) {

        $result = array();

        $cdir = scandir($dir);

        foreach ($cdir as $key => $value)
        {
            if (!in_array($value,array(".","..")))
            {
                if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
                {
                    $result[$value] = $this->dirToArray($dir . DIRECTORY_SEPARATOR . $value);
                }
                else
                {
                    $result[] = $value;
                }
            }
        }
        return $result;
    }
}

And inside my blade I just tried to call it like this:

{{ $watchFolder }}

but it did not work, I get the following error:

htmlentities() expects parameter 1 to be string, array given

Edit: The array I get shows all Folders/Files with subfolder in a directory. (used dd())

Currently it looks like this:

array:6 [▼
  123123 => array:2 [▼
    "subfolder1" => array:1 [▼
      0 => "video.mpg"
    ]
    "subfolder2" => array:1 [▶]
  ]
  789 => array:2 [▶]
  "folder1" => array:2 [▶]
  "folder2" => array:2 [▶]
  "folder3" => array:2 [▶]
  "folder1" => []
]
like image 296
John Does Legacy Avatar asked Mar 16 '16 07:03

John Does Legacy


People also ask

How do I display an array in blade?

You can access the array in the laravel blade using ['key']. Here are some examples like {{ $user['name'] }} and {{ $users[0]['name'] }}.

How do I view an array in PHP?

To display array structure and values in PHP, we can use two functions. We can use var_dump() or print_r() to display the values of an array in human-readable format or to see the output value of the program array.


4 Answers

To just output the array in blade for debugging purposes:

        @php
            var_dump($arr);
        @endphp

Then use a foreach loop as mentioned.

like image 86
Andrew Avatar answered Oct 20 '22 20:10

Andrew


From your question it appears as if you want to output the array for debugging purposes which as I have commented you can simply use <?php and ?> tags within your blade templates.

<?php dd($array); ?>

However, blade has a raw output tag pair which is {!! and !!}.

Once you're ready to display the output of your directory structure you should assign it to your view.

There are 2 quick ways of using it either with Reflection; inside your controller:

class WatchController extends Controller
{
    public function index(\Illuminate\Filesystem\Filesystem $filesystem)
    {
        $files = $filesystem->allFiles($watchFolderPath);

        // Your code.

        return view('name', ['files' => $files]);
    }
}

Or to call it from the container:

class WatchController extends Controller
{
    public function index()
    {
        $files = app('files')->allFiles($watchFolderPath);

        // Your code.

        return view('name', ['files' => $files]);
    }
}

Also you should be using the Filesystem, there is no need to reinvent the wheel – You should read the manual.

like image 42
Ash Avatar answered Oct 20 '22 18:10

Ash


You should use @foreach:

@foreach ($watchFolder as $key => $value)
    Key: {{ $key }}    
    Value: {{ $value }} 
@endforeach

Or

@foreach ($watchFolder as $w)
    {{ $w->name }}    
@endforeach
like image 8
Alexey Mezenin Avatar answered Oct 20 '22 18:10

Alexey Mezenin


For those that might come looking...like me

echo implode("\n",$array);

else if its multidimensional array

echo implode("\n",array_collapse($array));
like image 4
Letlhogonolo Letlape Avatar answered Oct 20 '22 18:10

Letlhogonolo Letlape