从集合Laravel中删除重复属性



出于某种奇怪的原因,我的集合(从查询返回)正好显示重复的属性4 times。有没有一种方法可以删除重复或重复的属性。

#attributes: array:2 [▼
        "name" => "Ram Inden"
        "features" => "2,2,2,2,3,3,3,3,4,4,4,4"

在此集合中,功能属性仅应具有2,3,4,但重复四次,我不知道为什么。在我的数据库中,仅是2,3,4

另一件事,这件事只会出现在Live服务器上,我的Localhost工作正常。

任何帮助都将不胜感激

使用php爆炸,array_uniqe,join函数

 $collection = collect([
            "name" => "Ram Inden",
            "features" => "2,2,2,2,3,3,3,3,4,4,4,4"
        ]);
        $unique = join(',', array_unique(explode(',', $collection['features'])));
        $collection['features'] = $unique;
        return $collection;

从集合开始,您可以通过以下方式消除重复项:

$collection = collect([
    "name" => "Ram Inden",
    "features" => "2,2,2,2,3,3,3,3,4,4,4,4"
]);
$str = $collection['features'];  
$collection->prepend(implode(',',array_unique(explode(',', $str))) ,'features');
 return $collection;

最新更新