search
HomeBackend DevelopmentPHP Tutorial请问一个关于PHP大数组去重的有关问题

请教一个关于PHP大数组去重的问题
请教一个问题,关于PHP大数组操作,一张表有几百万的数据要拿到PHP数组中做去重操作:
例如:id 性别 身份证三个字段,需要统计男女各有多少人(有其它特定逻辑,不能在MySQL中去重)
实现方法:id是自增的,每次按id取5w条数据,拿到一个数组中做去重操作
$count = array(
'男'  => array(
            '身份证1'  => 1,
    '身份证2'  => 1,
    ....
),
'女' => ...
);
最后看男女下共有多少个身份证即为去重后的数据
问题:随着数组越来越大,去重速度也越来越慢,不知道有没有其它解决方案或者优化方法,来请教一下,thx!
------解决思路----------------------
我们按你给出的数据做一个测试

drop table if exists play;<br /><br />CREATE TABLE `play` (<br />  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,<br />  `time` int(10) NOT NULL,<br />  `uid` int(10) unsigned NOT NULL,<br />  `game` varchar(255) NOT NULL,<br />  `channel` varchar(255) NOT NULL,<br />  `system` varchar(255) NOT NULL,<br />  `screen` varchar(255) NOT NULL,<br />  `network` varchar(255) NOT NULL,<br />  PRIMARY KEY (`id`),<br />  KEY `datetime` (`time`)<br />) charset=gbk;<br /><br />insert into play values<br />(1,1421812389,10000,'所有游戏-魔兽世界-0服','360-360联盟','WIN7','1024x768','电信'),<br />(2,1421812389,10001,'所有游戏-魔兽世界-1服','网易-网易联盟','XP','1366x768','联通'),<br />(3,1421812389,10000,'所有游戏-魔兽世界-0服','360-360联盟','WIN7','1024x768','电信');<br /><br />drop table if exists play_game;<br /><br />create table play_game ( game varchar(100) ) charset=gbk;<br /><br />insert into play_game values ('所有游戏'),('魔兽世界'),('0服'),('1服');<br /><br />drop table if exists play_channel;<br /><br />create table play_channel ( channel varchar(100) ) charset=gbk;<br /><br />insert into play_channel values ('360'),('360联盟'),('网易'),('网易联盟');<br /><br />select a.id, a.time, a.uid, b.game, c.channel, a.system, a.screen, a.network from play a, play_game b, play_channel c where <br />  find_in_set(b.game, replace(a.game, '-', ','))<br />  and<br />  find_in_set(c.channel, replace(a.channel, '-', ','))<br />
可得到这样的结果
<br />id time    uid  game   channel system screen  network <br />1  1421812389 10000 所有游戏 360   WIN7  1024x768 电信 <br />3  1421812389 10000 所有游戏 360   WIN7  1024x768 电信 <br />1  1421812389 10000 魔兽世界 360   WIN7  1024x768 电信 <br />3  1421812389 10000 魔兽世界 360   WIN7  1024x768 电信 <br />1  1421812389 10000 0服    360   WIN7  1024x768 电信 <br />3  1421812389 10000 0服    360   WIN7  1024x768 电信 <br />1  1421812389 10000 所有游戏 360联盟 WIN7  1024x768 电信 <br />3  1421812389 10000 所有游戏 360联盟 WIN7  1024x768 电信 <br />1  1421812389 10000 魔兽世界 360联盟 WIN7  1024x768 电信 <br />3  1421812389 10000 魔兽世界 360联盟 WIN7  1024x768 电信 <br />1  1421812389 10000 0服    360联盟 WIN7  1024x768 电信 <br />3  1421812389 10000 0服    360联盟 WIN7  1024x768 电信 <br />2  1421812389 10001 所有游戏 网易   XP   1366x768 联通 <br />2  1421812389 10001 魔兽世界 网易   XP   1366x768 联通 <br />2  1421812389 10001 1服    网易   XP   1366x768 联通 <br />2  1421812389 10001 所有游戏 网易联盟 XP   1366x768 联通 <br />2  1421812389 10001 魔兽世界 网易联盟 XP   1366x768 联通 <br />2  1421812389 10001 1服    网易联盟 XP   1366x768 联通 

再从这个结果出发,还有什么是不可用 SQL 做到的呢?

如果你永久性的将 所有游戏-魔兽世界-0服 改为 所有游戏,魔兽世界,0服 那就不需要在查询时执行 replace 函数了(当然这可能会涉及程序的改动),效率自然会有所提高
如果你再将最后的查询定义成视图的话,效率就又会提高不少(视图中如果一条记录的源数据没有被改变,则不做查询动作而直接返回缓存的结果)

------解决思路----------------------
怎么能把  所有游戏-wow-1服   存在一个字段里呢~
我是建议添加几个字段,将它拆开保存,然后在mysql上排重

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you set the session cookie parameters in PHP?How do you set the session cookie parameters in PHP?Apr 22, 2025 pm 05:33 PM

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

What is the main purpose of using sessions in PHP?What is the main purpose of using sessions in PHP?Apr 22, 2025 pm 05:25 PM

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How can you share sessions across subdomains?How can you share sessions across subdomains?Apr 22, 2025 pm 05:21 PM

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.

How does using HTTPS affect session security?How does using HTTPS affect session security?Apr 22, 2025 pm 05:13 PM

HTTPS significantly improves the security of sessions by encrypting data transmission, preventing man-in-the-middle attacks and providing authentication. 1) Encrypted data transmission: HTTPS uses SSL/TLS protocol to encrypt data to ensure that the data is not stolen or tampered during transmission. 2) Prevent man-in-the-middle attacks: Through the SSL/TLS handshake process, the client verifies the server certificate to ensure the connection legitimacy. 3) Provide authentication: HTTPS ensures that the connection is a legitimate server and protects data integrity and confidentiality.

The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.