Php 过滤器Laravel集合不工作
我有一个名为$api_items的集合。我要删除此集合的所有空值:Php 过滤器Laravel集合不工作,php,laravel,laravel-5,laravel-5.2,laravel-5.3,Php,Laravel,Laravel 5,Laravel 5.2,Laravel 5.3,我有一个名为$api_items的集合。我要删除此集合的所有空值: Collection {#365 ▼ #items: array:2 [▼ 0 => Article {#342 ▼ +user_id: null +article_id: 1304 +family_id: null +active: true +creation_date: null +name: "CUENTO "AMIGOS/AS P
Collection {#365 ▼
#items: array:2 [▼
0 => Article {#342 ▼
+user_id: null
+article_id: 1304
+family_id: null
+active: true
+creation_date: null
+name: "CUENTO "AMIGOS/AS PARA SIEMPRE" ESTANDAR"
+description: null
+detail: null
+constructor_name: null
+stock_available: true
+stock: null
+prices: array:4 [▶]
}
1 => Article {#347 ▼
+user_id: null
+article_id: 1885
+family_id: null
+active: true
+creation_date: null
+name: "CUENTO "AMIGOS/AS PARA SIEMPRE" LUXE"
+description: null
+detail: null
+constructor_name: null
+stock_available: true
+stock: null
+prices: array:4 [▶]
}
我使用每个方法来过滤:
$filtered = $api_items->each(function ($item, $key) {
if($item != null) {
return $item;
}
});
但是$filtered再次返回空值…I假设两个集合的大小始终相同,则解决方案如下:
$nested_result = [];
foreach($api_items as $index => $item){
$item->name = $seo_items[$index]->name;
$item->description = $seo_items[$index]->description;
$item->detail = $seo_items[$index]->detail;
$nested_result = $item;
}
然后使用方法从$nested\u结果
数组创建集合实例:
$nested_collection = collect($nested_result);
希望这有帮助。这是使用代码
$newArray = array();
foreach($data as $key => $inner_array) {
$ke = [];
foreach($inner_array as $key_v => $inner_array_v){
if($inner_array_v != 'N/A'){
$ke[] = $inner_array_v;
}
}
$newArray[] = $ke;
}
如果要从数组中删除项目,请尝试使用
filter()
函数。我还建议尝试内联使用真理陈述,而不使用$key/$value对
$filtered = $api_items->filter(function ($item) {
return $item !== null;
});
默认情况下,过滤器将查看这些值,最终得到一个更紧凑的语句