Laravel集合-删除包含空数组的对象
我有一个对象集合,其中一些包含空数组Laravel集合-删除包含空数组的对象,laravel,filter,collections,Laravel,Filter,Collections,我有一个对象集合,其中一些包含空数组 object(Illuminate\Support\Collection)#34225 (1) { ["items":protected]=> array(0) { } } object(Illuminate\Support\Collection)#34226 (1) { ["items":protected]=> array(0) { } } object(Illuminate\Support\Collection)#3
object(Illuminate\Support\Collection)#34225 (1) {
["items":protected]=>
array(0) {
}
}
object(Illuminate\Support\Collection)#34226 (1) {
["items":protected]=>
array(0) {
}
}
object(Illuminate\Support\Collection)#34227 (1) {
["items":protected]=>
array(0) {
}
}
object(Illuminate\Support\Collection)#34228 (1) {
["items":protected]=>
array(0) {
}
}
object(Illuminate\Database\Eloquent\Collection)#23760 (1) {
["items":protected]=>
array(2) {
[0]=>
object(App\Models\File)#23766 (27) {
["primaryKey":protected]=>
string(6) "FileID"
["table":protected]=>
我可以使用someone帮助过滤对象集合,以便删除包含空数组的对象
因此,剩下的就是具有非空数组的对象
object(Illuminate\Database\Eloquent\Collection)#23760 (1) {
["items":protected]=>
array(2) {
[0]=>
object(App\Models\File)#23766 (27) {
["primaryKey":protected]=>
string(6) "FileID"
我已使用
$things = $foos->get($thing->ID, collect());
非常感谢您的帮助您可以使用
toArray()
方法将其转换为数组
$things = $foos->get($thing->ID, collect())->toArray();
foreach($things as $thing) {
if(empty($thing['items'])) {
unset($thing);
}
}
$things = array_values($things);
或
使用过滤器()
filter方法使用给定回调筛选集合,仅保留通过给定真值测试的项:
$things = $foos->get($thing->ID, collect());
$filtered = $things->filter(function ($value, $key) {
return !empty($value->items) ;
});
$result = $filtered->all();
集合()具有方法筛选器
$collection = collect([
[],
['1'],
[],
]);
$collection->filter(); // will return collection with only [ [1] ]
仍然包含带有数组的项(0){}现在我有了一个数组,但仍然包含空数组
array(0){}array(2){[0]=>object(App\Models\File){[23766(27){[“primaryKey”:protected]=>string(6)“FileID”[“table”:protected]=>
我希望删除所有数组(0)项目。很高兴提供帮助,如果此答案解决了您的问题,请单击答案旁边的复选标记将其标记为已接受。有关更多信息,请参阅