2016-12-16 14 views
1

私は$ api_itemsというコレクションを持っています。フィルタリングするための各メソッドを使用してフィルタLaravel 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 [▶] 

    } 

- 私:

$filtered = $api_items->each(function ($item, $key) { 
    if($item != null) { 
     return $item; 
    } 
}); 

しかし、フィルタ$が再びNULL値...

答えて

1
を私に返して、私はこのコレクションのすべてのnull値を削除したいです

$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; 
} 

のTh:

私は両方のコレクションは、常に同じ大きさを持っていることをassum、解決策は次のようになります

$nested_collection = collect($nested_result); 

・ホープ、このことができます:エン$nested_result配列からコレクションのインスタンスを作成するためにcollect()メソッドを使用します。

0

これは、あなたの配列から項目を取り除くしようとしている場合は、filter()機能を使用してみてくださいなコードを使用し

$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; 
} 
0

です。私はまた、あなたの真実の声明をインラインで使用しようと提案すると、フィルターが値になりますデフォルトでは$キー/ $値のペア

$filtered = $api_items->filter(function ($item) { 
    return $item !== null; 
}); 

のない、あなたはよりコンパクトな文で終わるでしょう。

関連する問題