过滤器 Laravel Collection 无效
Filter Laravel Collection not works
我有一个名为 $api_items 的 collection。我想删除此 collection:
的所有空值
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 [▶]
}
我正在使用 each 方法进行过滤:
$filtered = $api_items->each(function ($item, $key) {
if($item != null) {
return $item;
}
});
但是 $filtered return 我又是空值...
我假设这两个集合的大小始终相同,解决方案类似于:
$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;
}
然后使用collect()
方法从$nested_result
数组创建集合实例:
$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;
});
默认情况下,过滤器将查看值,您最终会得到一个更紧凑的语句。
我有一个名为 $api_items 的 collection。我想删除此 collection:
的所有空值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 [▶]
}
我正在使用 each 方法进行过滤:
$filtered = $api_items->each(function ($item, $key) {
if($item != null) {
return $item;
}
});
但是 $filtered return 我又是空值...
我假设这两个集合的大小始终相同,解决方案类似于:
$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;
}
然后使用collect()
方法从$nested_result
数组创建集合实例:
$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;
});
默认情况下,过滤器将查看值,您最终会得到一个更紧凑的语句。