search
HomeBackend DevelopmentPHP Tutorial怎么快速找出两个数组中相同的元素

怎样快速找出两个数组中相同的元素?
例如我有两个数组 
1、('csdn','php','asp','msdn','c#','java','jsp','vb')
2、('javajia','asp','java','source','c#','delphi','jsp','vc++')

快速找出两个数组中相同的元素,要快速,不能一个个遍历,因为有几千个元素呢。
如上两个数组,得出结果是('asp','java','c#','jsp')

100分敬上!先谢了!!


------解决方案--------------------

你试试用正则表达式!

如还有其他需要请留言!
------解决方案--------------------

<br /><br />$a=array('csdn','php','asp','msdn','c#','java','jsp','vb');<br />$b=array('javajia','asp','java','source','c#','delphi','jsp','vc++');<br /><br />print_r(array_intersect($a,$b));<br />


//效率不知道怎么样
------解决方案--------------------
楼上正解!
------解决方案--------------------
求两个数组的交集
<br />$a=array('csdn','php','asp','msdn','c#','java','jsp','vb');<br />$b=array('javajia','asp','java','source','c#','delphi','jsp','vc++');<br />print_r(array_intersect($a,$b));<br />



还不明白去查下手册array_intersect的用法



------解决方案--------------------

引用:
PHP code

$a=array('csdn','php','asp','msdn','c#','java','jsp','vb');
$b=array('javajia','asp','java','source','c#','delphi','jsp','vc++');

print_r(array_intersect($a,$b));


 对啊,就这个函数就可以了
------解决方案--------------------
引用:
PHP code


$a=array('csdn','php','asp','msdn','c#','java','jsp','vb');
$b=array('javajia','asp','java','source','c#','delphi','jsp','vc++');

print_r(array_intersect($a,$b));



//效率不知道怎么样


+1
------解决方案--------------------
计算两数组的交集
------解决方案--------------------
仅需要取相同的值时,可以用array_intersect()如果连键名也需要比较时用array_intersect_assoc()
------解决方案--------------------
就是用这个函数,系统函数比自定义的效率要高很多

引用:
PHP code


$a=array('csdn','php','asp','msdn','c#','java','jsp','vb');
$b=array('javajia','asp','java','source','c#','delphi','jsp','vc++');

print_r(array_intersect($a,$b));



//效率不知道怎么样

------解决方案--------------------
1楼正解
------解决方案--------------------
我想来捞分的 没想到3楼太犀利。。。 正解
如果是C++ 我还有更犀利的方法 嘿嘿
------解决方案--------------------
3楼正解啊~~ 同样学习一下
------解决方案--------------------
3楼给力,学习了。
------解决方案--------------------
我也来默写一遍:array_intersect()


哈哈,记住了。。。。
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
使用C#中的Array.Sort函数对数组进行排序使用C#中的Array.Sort函数对数组进行排序Nov 18, 2023 am 10:37 AM

标题:C#中使用Array.Sort函数对数组进行排序的示例正文:在C#中,数组是一种常用的数据结构,经常需要对数组进行排序操作。C#提供了Array类,其中有Sort方法可以方便地对数组进行排序。本文将演示如何使用C#中的Array.Sort函数对数组进行排序,并提供具体的代码示例。首先,我们需要了解一下Array.Sort函数的基本用法。Array.So

带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

简单明了的PHP array_merge_recursive()函数使用方法简单明了的PHP array_merge_recursive()函数使用方法Jun 27, 2023 pm 01:48 PM

在进行PHP编程时,我们常常需要对数组进行合并。PHP提供了array_merge()函数来完成数组合并的工作,不过当数组中存在相同的键时,该函数会覆盖原有的值。为了解决这个问题,PHP在语言中还提供了一个array_merge_recursive()函数,该函数可以合并数组并保留相同键的值,使得程序的设计变得更加灵活。array_merge

如何使用PHP中的array_combine函数将两个数组拼成关联数组如何使用PHP中的array_combine函数将两个数组拼成关联数组Jun 26, 2023 pm 01:41 PM

在PHP中,有许多强大的数组函数可以使数组的操作更加方便和快捷。当我们需要将两个数组拼成一个关联数组时,可以使用PHP的array_combine函数来实现这一操作。这个函数实际上是用来将一个数组的键作为另一个数组的值,合并成一个新的关联数组。接下来,我们将会讲解如何使用PHP中的array_combine函数将两个数组拼成关联数组。了解array_comb

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

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.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment