我怎样才能从多维数组多次出现? [英] How can I get multiple occurences from multi dimensional array?
本文介绍了我怎样才能从多维数组多次出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到的记录从我的数据库,像这样,
I get records from my database like so,
$q = $dbc -> prepare("SELECT id, username, loginIP FROM accounts");
$q -> execute();
while ($accounts = $q -> fetch(PDO::FETCH_ASSOC)) {
var_dump($accounts);
}
这让我所有的用户名和ID,以及用户的IPS,
This gets me all the usernames and id and ips of users,
我如何检查IPS的多次出现?该IP使用 ip2long
保存,我曾尝试以下,
How can I check for multiple occurences of IPS? The IP are stored using ip2long
, I have tried the following,
$multiple = array_diff($accounts, array_unique($accounts));
var_dump($multiple);
但它没有工作?
推荐答案
另一个版本 - 使用SQL请求,找到重复的。事情是这样的:
Alternative version - use SQL request to find the duplicates. Something like this:
$q = $dbc -> prepare("SELECT a.id, a.username, a.loginIP FROM accounts a,
(SELECT loginIP FROM accounts GROUP BY loginIP HAVING COUNT(*)>1) d
WHERE d.loginIP = a.loginIP");
$q -> execute();
$accounts = array();
while ($acc = $q -> fetch(PDO::FETCH_ASSOC)) {
$accounts[$acc['loginIP']] = $acc;
}
var_dump($accounts);
只有副本会出现在$帐户采用IPS作为数组键。
Only duplicates will appear in $accounts with IPs as array keys.
这篇关于我怎样才能从多维数组多次出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文