Laravel集合如何从所有项目中删除特定键?

Laravel集合如何从所有项目中删除特定键?

问题描述:

It sounded crazy simple to me at first but I can't find the solution!

Here is My collection :

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
    ]
    .
    .
    .

But I want it to be like this :

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
    ]
    .
    .
    .

How can it be done with laravel collection? Do I have to change my collection to array and do the manipulation myself? How?

起初听起来很简单,但我找不到解决办法! p>

这是我的收藏: p>

  Collection {#433 
 #items:array:432 [
 0 => 数组:5 [
“word_id”=>  12218 
“name”=>  “ordered”
“rank”=>  12217 
“is_real”=>  1 
“id”=>  1 
] 
 1 => 数组:5 [
“word_id”=>  12097 
“name”=>  “one-dimensional”
“rank”=>  12096 
“is_real”=>  1 
“id”=>  2 
] 
 2 => 数组:5 [
“word_id”=>  19679 
“name”=>  “watery”
“rank”=>  19678 
“is_real”=>  1 
“id”=>  3 
] 
。
。
。
  code>  pre> 
 
 

但我希望它是这样的: p>

   Collection {#433 
 #items:array:432 [
 0 => 数组:5 [
“name”=>  “ordered”
“id”=>  1 
] 
 1 => 数组:5 [
“name”=>  “one-dimensional”
“id”=>  2 
] 
 2 => 数组:5 [
“name”=>  “watery”
“id”=>  3 
] 
。
。
。
  code>  pre> 
 
 

如何使用laravel集合完成? 我是否必须将我的集合更改为数组并自行进行操作? 怎么样? p> div>

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

Or you can also use

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