【发布时间】:2018-01-29 12:17:16
【问题描述】:
我有以下收藏
Collection {#430 ▼
#items: array:54 [▼
0 => {#435 ▼
+"name": "Persona 5"
+"cover": "cover_persona-5_playstation-3.png"
+"bio": "This is a syno"
+"all_nb_rank": null
+"platform_name": "Sony Playstation 3"
+"tag_name": "RPG"
+"developper": "Deep Silver"
+"publisher": "Atlus"
}
1 => {#437 ▼
+"name": "Persona 5"
+"cover": "cover_persona-5_playstation-3.png"
+"bio": "This is a syno"
+"all_nb_rank": null
+"platform_name": "Sony Playstation 3"
+"tag_name": "Turn based"
+"developper": "Deep Silver"
+"publisher": "Atlus"
}
2 => {#436 ▼
+"name": "Persona 5"
+"cover": "cover_persona-5_playstation-3.png"
+"bio": "This is a syno"
+"all_nb_rank": null
+"platform_name": "Sony Playstation 3"
+"tag_name": "Simulation"
+"developper": "Deep Silver"
+"publisher": "Atlus"
}
//
我想进入另一个集合的“tag_name”行,然后从主集合中删除它,并删除重复的值以获得类似的东西:
Collection {#430 ▼
#items: array:1 [▼
0 => {#435 ▼
+"name": "Persona 5"
+"cover": "cover_persona-5_playstation-3.png"
+"bio": "This is a syno"
+"all_nb_rank": null
+"platform_name": "Sony Playstation 3"
+"developper": "Deep Silver"
+"publisher": "Atlus"
}
Collection {#490 ▼
#items: array:3 [▼
0 => "RPG"
1 => "Turn based"
2 => "Simulation"
]
}
我已经设法通过使用将“tag_name”行放入另一个集合中
$tags = $collection->pluck('tag_name');
我还计划使用 unique() 收集方法来合并重复值。
但我不知道应该如何处理才能从主集合中删除“tag_name”。
我尝试使用 forget() 和 slice() 方法,但它不起作用。我知道我可以把它变成一个简单的数组,然后使用 PHP unset() 函数,但我想知道如何使用 Laravel 集合方法来做到这一点
【问题讨论】:
-
你可以通过做小技巧来使用忘记,看我的回答
标签: laravel laravel-5 laravel-collection