foreach array_unique 不起作用

问题描述 投票:0回答:1

我有这个代码:

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

返回空。 我希望数组

$file
仅显示 DISTINCT
$file['file_name']

php laravel foreach laravel-blade array-unique
1个回答
0
投票

我认为最好在你的控制器中解决这个问题,试试这个:

//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

我希望这对你有用

© www.soinside.com 2019 - 2024. All rights reserved.