2

I have this array coming form a DB PDO result, where users have made Hello there, I have this array coming form a DB PDO result, where users have made some feedback so it can be duplicated projects because a feedback can be either a comment or a suggestion

My DB Query

$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);

My Results

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...
    )

)

As you can see, I got repeated projects, but I need to get rid of duplicated projects

Can anyone help me out to extract only one project and not duplicated items

I just want to get projects 25, 27 and 28

Thanks!!

Edited: Added Mysql tag + the for loop

$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);
Jorge
  • 79
  • 9
  • 1
    Possible duplicate of [How to remove duplicate values from a multi-dimensional array in PHP](https://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – Spoody Sep 24 '18 at 23:20
  • Please add mysql tag to the question. – maljukan Sep 24 '18 at 23:25
  • @Mehdi tried this, with no luck, based on you link $this->json = array_intersect_key($this->json, array_unique((array_map('unserialize', $this->json)))); – Jorge Sep 24 '18 at 23:34
  • @maljukan Updated Question as you suggested – Jorge Sep 24 '18 at 23:35
  • You need to use `$this->results = array_map("unserialize", array_unique(array_map("serialize", $this->results)));` i.e. prior to json_encoding. – Nick Sep 25 '18 at 00:21
  • @Nick I did what you said and I only got one result, the first project duplicated. Thanks anyway – Jorge Sep 25 '18 at 00:29
  • It works fine for me: http://rextester.com/NMP6505 – Nick Sep 25 '18 at 00:45

1 Answers1

2

in SQL query add DISTINCT just after SELECT

Artyom Shegeda
  • 313
  • 1
  • 7