[英]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:我想在我的 .blade.php 中显示一个数组,但它不能正常工作,所以我的控制器看起来像这样:
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
htmlentities() 期望参数 1 是字符串,给定数组
Edit: The array I get shows all Folders/Files with subfolder in a directory.编辑:我得到的数组显示了目录中带有子文件夹的所有文件夹/文件。 (used dd())
(使用 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" => []
]
To just output the array in blade for debugging purposes:仅出于调试目的在刀片中输出数组:
@php
var_dump($arr);
@endphp
Then use a foreach loop as mentioned.然后使用前面提到的 foreach 循环。
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
和?>
标记。
<?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;有 2 种快速方法可以将其与 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 .此外,您应该使用文件系统,无需重新发明轮子——您应该阅读手册。
For those that might come looking...like me对于那些可能看起来......像我这样的人
echo implode("\n",$array);
else if its multidimensional array否则,如果它的多维数组
echo implode("\n",array_collapse($array));
您可以简单地使用:
@dump($arr)
To output your $watchFolder
array in a blade file, you can use the following method:要在刀片文件中输出
$watchFolder
数组,可以使用以下方法:
<pre><code>{{ json_encode($watchFolder, JSON_PRETTY_PRINT) }}</code></pre>
This will display a nicely formatted JSON version of your array and make it more readable:这将显示您的数组的格式良好的 JSON 版本并使其更具可读性:
{
"123123": {
"subfolder1": [
"video.mpg"
],
"subfolder2": []
},
"789": [],
"folder1": [],
"folder2": [],
"folder3": []
}
You can also remove the JSON_PRETTY_PRINT
if you only need to display it in a single line:如果您只需要在一行中显示它,您也可以删除
JSON_PRETTY_PRINT
:
{"123123":{"subfolder1":["video.mpg"],"subfolder2":[]},"789":[],"folder1":[],"folder2":[],"folder3":[]}
dd($result) ;
Its works!其作品! !!!!!
!!!!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.