创建一个没有重复元素的数组

I want to create an array with no repetitive element. I created an empty array at first, but it's meaningless to do that in PHP. A better way?

Table1:

Id Name

Table2:

Id Year PROJECT DURATION
1  2012  A      3
1  2012  B      2

Get data:

$sql = $mysqli->prepare(SELECT Name, 
            Table1.Id, 
            Year
            FROM Table1 JOIN Table2 
            WHERE Table1.Id=Table2.Id
            ORDER BY Id);
$sql->execute();
$result = $sql->get_result();

$Id[]=array();

while($row = $result->fetch_assoc()){

    if (!in_array($row["Id"], $Id)){
        $Id[]=$row["Id"];
        ...
    }
}

I do not use DISTINCT because I need all year, project and duration entries later. I could change the database. How to modify and use unique Id in table2? Thanks for the suggestions.

Try this PHP function:

http://php.net/manual/en/function.array-unique.php

It sounds like it will do what you want if you want to use PHP. Otherwise, use the DISTINCT keyword in SQL as @zerkms already suggested.

I would say it is not exactly a duplicate, because OP maybe didn't know about set data structures, a concept available in many languages, but there is another question about it in Stack Oveflow:

Built in support for sets in PHP?

To explain, a set data structure is a data type that holds another elements (like arrays), but guarantees they are unique within the set, usually guaranteeing a low complexity of insertion, deletion and search (O(1) or O(log N), depending on the implementation).

For what it seems from the link, PHP closest thing to sets (and its complexity guarantees) is to use the index key of the array to store the data, instead of in the array itself.

But if you don't bother with complexities (not advisable if your data set is too large or you perform too many insertion/deletion/search) you can just use array_unique, as also mentioned in the link.