2017-07-28 69 views
0

起初我聽起來很瘋狂,但我找不到解決方案!Laravel集合如何從所有項目中刪除特定的鍵?

下面是我收集:

Collection {#433 
    #items: array:432 [ 
    0 => array:5 [ 
     "word_id" => 12218 
     "name" => "ordered" 
     "rank" => 12217 
     "is_real" => 1 
     "id" => 1 
    ] 
    1 => array:5 [ 
     "word_id" => 12097 
     "name" => "one-dimensional" 
     "rank" => 12096 
     "is_real" => 1 
     "id" => 2 
    ] 
    2 => array:5 [ 
     "word_id" => 19679 
     "name" => "watery" 
     "rank" => 19678 
     "is_real" => 1 
     "id" => 3 
    ] 
    . 
    . 
    . 

但我希望它是這樣的:

Collection {#433 
    #items: array:432 [ 
    0 => array:5 [ 
     "name" => "ordered" 
     "id" => 1 
    ] 
    1 => array:5 [ 
     "name" => "one-dimensional" 
     "id" => 2 
    ] 
    2 => array:5 [ 
     "name" => "watery" 
     "id" => 3 
    ] 
    . 
    . 
    . 

如何將其與laravel集合做什麼?我是否必須將收藏集更改爲數組並自己進行操作?怎麼樣?

回答

2
$collection = $collection->map(function ($item) { 
    return array_only($item, ['id', 'name']); 
}); 
+1

謝謝你,它的工作 –

+0

我不得不等待10分鐘,接受了答案提到,因爲你沒有問任何問題,到現在爲止,也許你不知道這一點! 2,5年沒有問題?! WOWW! –

相關問題