【问题标题】:Laravel PHP move collection item to the top of the collectionsLaravel PHP 将集合项移动到集合的顶部
【发布时间】:2021-09-12 14:08:45
【问题描述】:

这是以防万一其他人有同样的问题并且像我一样没有找到合适的答案来解决它。

我有一个必须过滤的集合,因此当某个值被传递时,活动项首先出现在集合中。

Illuminate\Database\Eloquent\Collection {
    0 => array:2 [
      "id" => 1
      "name" => "Bogan, Weissnat and Jenkins"
    ]
    1 => array:2 [
      "id" => 4
      "name" => "Grady-Barrows"
    ]
    2 => array:2 [
      "id" => 7
      "name" => "Howe and Sons"
    ]
    3 => array:2 [
      "id" => 3
      "name" => "Macejkovic-Altenwerth"
    ]
  ]
}

需要根据 URL 传递的id 将项目移到顶部

【问题讨论】:

  • 如果你展示一个输入和所需输出的例子会有所帮助(不需要大量数据,但它应该显示你想要实现的目标)
  • @NigelRen 感谢您的评论。我已经更新了问题。
  • 所提供的答案是否有帮助?如果您有这样做的声誉,您应该用 ▲ 所有答案 支持。然后,您应该标记接受 ✓ 最能回答您的问题的一个答案。 This will mark the question as "closed," and give you some reputation on the site。如果没有一个答案令人满意,请向 cmets 提供反馈,或编辑您的问题以澄清问题。

标签: php arrays laravel collections


【解决方案1】:

我就是这样解决的。

public function moveOtherToTop($collection, $key, $item)
{
    return $collection->reject(function ($value) use ($item){
        return $value[$key] == $item;
    })->prepend($collection->filter(function ($value) use ($item) {
        return $value[$key] == $item;
    })[$item]);
}

想法来自Laracast和Stillat的文章。

如果这个答案有更好的解决方案,欢迎提出建议。

【讨论】:

  • 对不起,这里的命名是问题
【解决方案2】:

您可以通过自定义函数简单地对集合进行排序:

use Illuminate\Support\Collection;

$data = collect([
    ["id" => 1, "name" => "Bogan, Weissnat and Jenkins"],
    ["id" => 4, "name" => "Grady-Barrows"],
    ["id" => 7, "name" => "Howe and Sons"],
    ["id" => 3, "name" => "Macejkovic-Altenwerth"],
]);

$key = "name";
$value = "Grady-Barrows";

public function moveFirst(Collection $data, string $key, mixed $value): Collection
{
    return $data->sortBy(fn($v) => $v[$key] !== $value);
}

它将为匹配条目返回 false (0),为其余条目返回 true (1),因此匹配条目被放在最前面。使用箭头函数可以使语法更简单。

【讨论】:

    【解决方案3】:

    我在 Laravel 5.8 中运行它

    use Illuminate\Support\Collection;
    
    function moveItemToTopCollection(Collection $collection, String $key, $value) :Collection
    {
        $item_to_first = null;
    
        // Search element to top
        foreach ($collection as $item) {
            if ($item->$key == $value){
                $item_to_first = $item;
                break;
            }
        }
        // If element not found, return original collection
        if (!$item_to_first){
            return $collection;
        }
        
    
        // Element to top, first remove of collection, then insert to top
        return $collection->reject(function ($value) use ($item_to_first){
            return $value == $item_to_first;
        })->prepend($item_to_first);
    }
    

    【讨论】:

    • 你能edit你的答案来解释你改变了什么以及为什么这应该解决这个问题吗?
    【解决方案4】:

    您可以使用出色的spatie/laravel-collection-macros 包中的prioritize 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 2017-07-18
      • 2021-08-25
      相关资源
      最近更新 更多