-1

In this function I'm filtering my DB output. I don't know how to get unique values only. My array:

[2] => Array (
    [name] => John
    [id] => D
    )
[4] => Array (
    [name] => Grace
    [id] => K
    )
[6] => Array (
    [name] => John
    [id] => U
    )

Now I want a new array which has to contain only the first "John" and "Grace", because they are 'unique'. What I've tried?

array_unique($filtredrow, "name");

But after that function I'm only receiving names and I have to get also ids of unique people.

Thank you for help!

EDIT This is what I want to get:

[2] => Array (
        [name] => John
        [id] => D
        )
    [4] => Array (
        [name] => Grace
        [id] => K
        )

My loading data in which I've tried not to save elements which are already in array.

$rows=$mydb->get_resoults("SELECT DISTINCT name, id FROM DB", ARRAY_A);
foreach($rows as $key=>$row)
    {
        if($row[name]!=null && $row[id]!=null)
        {
            if($filtredrow[$key][name]!=$row[name])
            {
                $filtredrow[$key][name]=$row[name];
                $filtredrow[$key][id]=$row[id];
            }
        }
    }

But it's saving all values :/ also duplicates

9
  • 2
    "I have to get also ids of unique people" You question is unclear. Please update to include examples of the results you're attempting to achieve. Commented Oct 22, 2018 at 22:35
  • 1
    And stackoverflow.com/questions/9409182/… and stackoverflow.com/questions/22524249/… Commented Oct 22, 2018 at 22:37
  • 2
    There are two ids for John. How do you decide which one to pick? Commented Oct 22, 2018 at 22:40
  • 1
    Then it really seems like the id doesn't matter? Why do you need the id at all if it's okay to just arbitrarily throw most of them out? Not trying to be difficult, just trying to understand. Commented Oct 22, 2018 at 22:46
  • 1
    Is it important to preserve the array keys? (Like you still have the original 2 and 4 in the example of what you're trying to get.) Commented Oct 22, 2018 at 22:56

1 Answer 1

1

I'll put this at the beginning, because I'd rather do this type of thing in the database if possible. You can change your query to get a result like the one you want.

SELECT name, MIN(id) FROM your_table GROUP BY name

If you need to do this in PHP for some other reason, here are a few different ways.

All of these involve assigning the row to your result array using the name as the key.

If you want to keep the first instance of each name, then as you iterate your input array, if a key matching the name of the row exists in the result array, don't add it. This way you'll end up with the first instance of the person's name in your result set.

foreach ($your_array as $person) {
    if (!isset($unique[$person['name']]) {
        $unique[$person['name']] = $person;
    }
}
$unique = array_values($unique);

If it doesn't matter which ID you get, then you can leave out the issetcheck. This way you'll end up with the last instance of each person's name in your result set because that key will be overwritten each time you encounter a duplicate.

foreach ($your_array as $person) {
    $unique[$person['name']] = $person;
}
$unique = array_values($unique);

If you need to keep all the ids associated with a name, append the id value to an array under the name key instead of adding the entire row.

foreach ($your_array as $person) {
    $people[$person['name']][] = $person['id];
}

This will give you a result like

['John' => ['D', 'U'], 'Grace' => ['K']];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.