从集合Laravel中删除重复的属性

时间:2017-11-27 15:06:59

标签: php mysql 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工作正常。

任何帮助都会非常感激

1 个答案:

答案 0 :(得分:0)

使用php explode,array_uniqe,join function

 $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;
相关问题