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.
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.
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'];
}
in_array() calls on an ever growing array is not efficient. array_column() then array_unique() is much more elegant and probably more efficient.Since PHP 5.5, a new function called array_column() is also available. You can use it following way:
$allEmails = array_column($arr, 'email');
$uniqueEmails = array_unique($allEmails);
Consider the same array but id of 3rd index is different:
$all_array = Array
(
[0] => Array
(
[id] => 1
[value] => 111
)
[1] => Array
(
[id] => 2
[value] => 222
)
[2] => Array
(
[id] => 3
[value] => 333
)
[3] => Array
(
[id] => 4
[value] => 111
)
)
Now, both 1 & 4 have same values. So we want to remove any of them:
$unique_arr = array_unique( array_column( $all_array , 'value' ) );
print_r( array_intersect_key( $all_array, $unique_arr ) );
$all_array. Thanks!Because PHP will not allow duplicated keys in the same level of an array, you can call:
$unique = array_column($arr, 'email', 'email');
This will create an associative array where the keys are the same as the values, and no duplicate values can possibly exist.
From this point, you call array_values() on that data (if you need an indexed array).
Note that any column values which will be modified by PHP when used as a key will not be reliable. For instance, if you have a null value, the key will be come 0 -- this will be prone to collision with non-null values which are converted to 0 when used as a key. Another example would be that 0.9 would be truncated to 0 when converted to a key. Of course, non-scalar values cannot be converted to keys -- so that type of scenario would not be suitable.
If you get the list sorted by email from SQL you can improve performance by looping through the array like Gareth does, but instead only compare current email address with the last inserted email address. Below is a code example for this:
$uniqueEmails = array();
$lastemail = '';
foreach($arr as $array)
{
if($array['email'] != $lastemail)
{
$uniqueEmails[] = $array['email'];
$lastemail = $array['email'];
}
}
If we consider this as our array.
array(5) {
[0]=>
array(2) {
["name"]=>
string(5) "Imran"
["email"]=>
string(13) "[email protected]"
}
[1]=>
array(2) {
["name"]=>
string(4) "Syed"
["email"]=>
string(12) "[email protected]"
}
[2]=>
array(2) {
["name"]=>
string(6) "Ertaza"
["email"]=>
string(14) "[email protected]"
}
[3]=>
array(2) {
["name"]=>
string(6) "Syed-2"
["email"]=>
string(12) "[email protected]"
}
[4]=>
array(2) {
["name"]=>
string(7) "Imran-2"
["email"]=>
string(13) "[email protected]"
}
}
And now if we want to get the unique value of the email column of the array, we can use array_unique()
But to get the unique values of the above arrary, we need perform 2 steps.
First, we need find out the list of all emails in an another array. We can do this by the following code.
$ListOfallEmails = array_column($users_details, 'email');
And the result will be this as array_column() also return an array.
array(5) {
[0]=>
string(13) "[email protected]"
[1]=>
string(12) "[email protected]"
[2]=>
string(14) "[email protected]"
[3]=>
string(12) "[email protected]"
[4]=>
string(13) "[email protected]"
}
So, now we have all the email list that contains duplicate email as well. This is the right time to perform another step to get the unique email address. We can do that with the php array_unique() built-in function.
$ListOfuniqueEmails = array_unique($ListOfallEmails);
By running the above code, we can get the list of unique email address list as an array. Please remember the array_unique() php function also returns an array.
// This is the output with unique email address.
array(3) {
[0]=>
string(13) "[email protected]"
[1]=>
string(12) "[email protected]"
[2]=>
string(14) "[email protected]"
}