Search code examples
phparraysunique

PHP Unique Values from Column in Array


I have an array that is generated from a SQL query that I run. It looks like the following:

$arr[$i]['id'] = $id;
$arr[$i]['name'] = $name;
$arr[$i]['email'] = $email;

How can I get the unique values from the email column? I appreciate the help.


Solution

  • Either filter it in your column using the DISTINCT method in MySQL, or use something like

    $uniqueEmails = array();
    foreach($arr as $array)
    {
        if(!in_array($array['email'], $uniqueEmails)
            $uniqueEmails[] = $array['email'];
    }