按值划分的唯一数组

时间:2015-11-20 10:52:27

标签: php arrays

$email_users[] = $row;

print_r($email_users);

结果:

Array ( 

[0] => Array ( 
[user_id] => 87436 
[username] => Admin 
[user_email] => email@online.us 
[user_lang] => de 
[allowed] => 1 ) 

[1] => Array ( 
[user_id] => 68013 
[username] => Testuser 
[user_email] => email2@online.us 
[user_lang] => de 
[allowed] => 1 ) 

[2] => Array ( 
[user_id] => 68013 
[username] => Testuser 
[user_email] => email2@online.us 
[user_lang] => de 
[allowed] => 1 ) 

)

如您所见,user_id 68013是双倍的。我必须删除双数组。 结果应如下所示

Array ( 

[0] => Array ( 
[user_id] => 87436 
[username] => Admin 
[user_email] => email@online.us 
[user_lang] => de 
[allowed] => 1 ) 

[1] => Array ( 
[user_id] => 68013 
[username] => Testuser 
[user_email] => email2@online.us 
[user_lang] => de 
[allowed] => 1 ) 

)

我阅读并尝试了一些我在堆栈上找到的解决方案。例如:

How to get unique value in multidimensional array

$email_users[] = $row;

$user_ids = array();
foreach ($email_users as $h) {
$user_ids[] = $h['user_id'];
}
$email_users = array_unique($user_ids);

print_r($email_users);

但是print_r只是:

Array ( [0] => 87436 [1] => 68013 )

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

您应该从查询中获得不同的价值。使用SELECT DISTINCT * FROM tablename,您将获得Distinct值。 或者使用$ email_users = array_unique($ email_users,SORT_REGULAR);