代码示例 #1
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SiteController extends Controller
{
public function index()
{
$students = collect([
["id" => 1, "name" => "Sanjay", "email" => "sanjay@gmail.com", "marks" => 88],
["id" => 2, "name" => "Vijay", "email" => "vijay@gmail.com", "marks" => 70],
["id" => 3, "name" => "Ashish", "email" => "ashish@gmail.com", "marks" => 75]
]);
$passed = $students->filter(function ($value, $key) {
return data_get($value, 'marks') > 70;
});
$passed = $passed->all();
dd($passed);
}
}
概念
$passed = $students->filter(function ($value, $key) {
return data_get($value, 'marks') > 70;
});
输出
代码示例 #2
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SiteController extends Controller
{
public function index()
{
$data = collect([2, 3, 5, null, false, '', 0, []]);
$data = $data->filter()->all();
dd($data);
}
}
在这种情况下,它将从数组中删除假值
评论区