从重复项中清除阵列

时间:2018-09-24 23:18:29

标签: php

我从DB PDO结果中获得了这个数组,在那儿用户打了个招呼,在那里,我从DB PDO结果中得到了这个数组,用户在这里做了一些反馈,因此可以重复项目,因为反馈既可以是评论或建议

我的数据库查询

$this->sql = $this->dbh->query("SELECT idProject, title, excerpt 
FROM projects AS up
JOIN comments AS co ON up.idProject = co.idProjectComment 
WHERE co.idUserComment = $id");
$this->results = $this->sql->fetchAll(PDO::FETCH_ASSOC);

我的结果

print_r($this->results);
Array
(
[0] => Array
    (
        [idProject] => 25
        [title] => Lorem Ipsum
        [excerpt] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut...
    )

[1] => Array
    (
        [idProject] => 25
        [title] => Lorem Ipsum
        [excerpt] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut...
    )

[2] => Array
    (
        [idProject] => 25
        [title] => Lorem Ipsum
        [excerpt] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut...
    )

[3] => Array
    (
        [idProject] => 27
        [title] => Lorem Ipsum
        [excerpt] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut...
    )

[4] => Array
    (
        [idProject] => 28
        [title] => Lorem Ipsum
        [excerpt] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut...
    )

[5] => Array
    (
        [idProject] => 28
        [title] => Lorem Ipsum
        [excerpt] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut...
    )

)

如您所见,我有重复的项目,但是我需要摆脱重复的项目

任何人都可以帮我提取一个项目而不提取重复的项目

我只想获得项目25、27和28

谢谢!

编辑:添加了Mysql标记+ for循环

$this->json['projects'] = array();

        for($i=0; count($this->results) > $i; $i++){

            $this->json['projects'][$i]['projectId']    = $this->results[$i]['idProject'];
            $this->json['projects'][$i]['title']        = $this->results[$i]['title'];
            $this->json['projects'][$i]['excerpt']      = $this->results[$i]['excerpt'];

        }

return json_encode($this->json, JSON_PRETTY_PRINT);

1 个答案:

答案 0 :(得分:2)

在SQL查询中,在SELECT之后添加DISTINCT