刀片 php 中的 Laravel 打印数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36029466/
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
Laravel print array in blade php
提问by John Does Legacy
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" => []
]
回答by ash
From your question it appears as if you want to output the arrayfor 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 !!}
.
但是,blade 有一个原始输出标签对,即{!!
和!!}
。
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:
有 2 种快速使用它的方法,或者与 Reflection 一起使用;在您的控制器内:
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 shouldbe using the Filesystem, there is no need to reinvent the wheel – You should read the manual.
此外,您应该使用Filesystem,无需重新发明轮子 - 您应该阅读手册。
回答by Andrew
To just output the array in blade for debugging purposes:
仅出于调试目的在刀片中输出数组:
@php
var_dump($arr);
@endphp
Then use a foreach loop as mentioned.
然后使用前面提到的 foreach 循环。
回答by Alexey Mezenin
回答by Letlhogonolo Letlape
For those that might come looking...like me
对于那些看起来像我一样的人
echo implode("\n",$array);
else if its multidimensional array
否则如果它的多维数组
echo implode("\n",array_collapse($array));