foreach array_unique不起作用



我有此代码:

@foreach($files['folders'] as $file)
  @foreach(array_unique($file['file_name']) as $names)
    <tr><td>{{$names}}</td></tr>
  @endforeach
@endforeach

返回null。我想要数组$file仅显示不同的$file['file_name']

我认为最好在控制器中解决此问题,请尝试以下操作:

//TheController.php
... whatever code you have in controller...
$seen = [];
$unique_files = [];
foreach($files['folders'] as $file)
    if(!isset($seen[$file['file_name']]) {
       $seen[$file['file_name'] = true;
       $unique_files[] = $file;
    }
}
$files['folders'] = $unique_files;
//Then pass $files as usual to your view

在模板文件中,您只有唯一的值:

@foreach($files['folders'] as $file)
   <tr><td>{{$file['file_name']}}</td></tr>
@endforeach

我希望这对您有用

相关内容

  • 没有找到相关文章

最新更新