-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

Sz3jdii
  • 507
  • 8
  • 23
  • 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. – Alex Howansky Oct 22 '18 at 22:35
  • @Nick I've just eddited my question – Sz3jdii Oct 22 '18 at 22:37
  • 1
    And https://stackoverflow.com/questions/9409182/remove-duplicate-value-in-multidimensional-array and https://stackoverflow.com/questions/22524249/remove-duplicate-values-from-a-multidimensional-array-in-php – Nick Oct 22 '18 at 22:37
  • 2
    There are two ids for John. How do you decide which one to pick? – Alex Howansky Oct 22 '18 at 22:40
  • I need to grab the first occurance so it's not neccessery to know 'who is who'. Just if John is in array leave him and don't add another Johns. – Sz3jdii Oct 22 '18 at 22:41
  • 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. – Don't Panic Oct 22 '18 at 22:46
  • @Don'tPanic It's quite difficult to explain. Let's say that we have to prepare a group of people that are unique of their names, they have different IDs and in that group we can only have one of each names. – Sz3jdii Oct 22 '18 at 22:50
  • 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.) – Don't Panic Oct 22 '18 at 22:56
  • No it's not It's just print_r output from my function – Sz3jdii Oct 22 '18 at 22:57

1 Answers1

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']];
Don't Panic
  • 41,125
  • 10
  • 61
  • 80