【问题标题】:laravel collection remove itemlaravel 集合删除项目
【发布时间】: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


【解决方案1】:

您可以通过一些小技巧来使用forget(),因为forget() 仅适用于集合对象。

$collections = $collections->map(function ($c) {
    return collect($c)->forget('tag_name');
});
return $collections;

【讨论】:

  • 虽然你在回答这个问题,但它最初是要求使用 Laravel 收集方法而不是 unset()。如果 Laravel 集合是 PHP 数组的超集,并且 unset() 用于从 Laravel 集合中删除属性,则应该指定它。
  • @SomethingOn 我错过了原始问题中未设置的部分。我已经更新了我的答案,感谢您的建议。
  • 这对我很有帮助,因为 except() 不起作用,但确实如此。
【解决方案2】:

except() 方法完全符合您的需要。它从集合中删除一个或多个键:

$persona = collect([     
    'name' => 'Persona 5',
    'cover' => 'cover_persona-5_playstation-3.png',
    'bio' => 'This is a syno',
    'all_nb_rank": nul',
    'platform_name' => 'Sony Playstation 3',
    'tag_name' => 'RPG',
    'developper' => 'Deep Silver',
    'publisher' => 'Atlus'
]);

$result = $persona->except(['tag_name']);

$personasWithOutTagName = $personas->except(['tag_name']);

这将导致:

[
    'name' => 'Persona 5',
    'cover' => 'cover_persona-5_playstation-3.png',
    'bio' => 'This is a syno',
    'all_nb_rank": nul',
    'platform_name' => 'Sony Playstation 3',
    'developper' => 'Deep Silver',
    'publisher' => 'Atlus'
];

对角色集合中的所有项目执行此操作,您可以执行此操作:

$result = $personas->map(function ($person) {
    return collect($person)->except(['tag_name'])->all();
});

$result 将是一个集合,其中包含除 tag_name 键之外的所有角色。

【讨论】:

    【解决方案3】:

    partition 方法可用于根据您的真值测试逻辑将一个集合分成一个或多个集合。将其与列表方法一起使用,如此处示例所示:

    https://laravel.com/docs/5.5/collections#method-partition

    另一个选项是 mapToGroups 方法。

    https://laravel.com/docs/5.5/collections#method-maptogroups

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      相关资源
      最近更新 更多