刀片php中的Laravel打印阵列
发布时间:2020-12-14 19:56:33 所属栏目:大数据 来源:网络整理
导读:我想在我的.blade.php中显示一个数组,但它无法正常工作,所以我的控制器看起来像这样: class WatchController extends Controller{ public function index() { $watchFolderPath = 'C:xampphtdocsProrec'; $watchFolder = $this-dirToArray($watch
我想在我的.blade.php中显示一个数组,但它无法正常工作,所以我的控制器看起来像这样:
class WatchController extends Controller { public function index() { $watchFolderPath = 'C:xampphtdocsProrec'; $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; } } 在我的刀片中,我只是试着这样称呼它: {{ $watchFolder }} 但它没有用,我收到以下错误:
编辑: 目前它看起来像这样: 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" => [] ]
从您的问题看来,似乎您想要输出数组用于调试目的,正如我所评论的那样,您只需使用<?php和?>您的刀片模板中的标记.
<?php dd($array); ?> 但是,刀片有一个原始输出标签对,它是{!!和!!}. 准备好显示目录结构的输出后,应将其分配给视图. 使用Reflection有2种快速使用方法;在你的控制器内: class WatchController extends Controller { public function index(IlluminateFilesystemFilesystem $filesystem) { $files = $filesystem->allFiles($watchFolderPath); // Your code. return view('name',['files' => $files]); } } 或者从container调用它: class WatchController extends Controller { public function index() { $files = app('files')->allFiles($watchFolderPath); // Your code. return view('name',['files' => $files]); } } 你应该使用Filesystem,没有必要重新发明轮子 – 你应该read the manual. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |