PHP输出数组中重名的元素的几种处理方法
1.可以直接用php的内置函数array_intersect()
array array_intersect ( array $array1 , array $array2 [, array $ ... ] )
array_intersect() 返回一个数组,该数组包含了所有在 array1 中也同时出现在所有其它参数数组中的值。注意键名保留不变。
代码:
复制代码 代码如下:
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
?>
输出结果:
Array( [a] => green [0] => red)
2.也可以自己写一个算法:
复制代码 代码如下:
function my_array_same($a){
$b = array_unique($a);
$r = array_diff_key($a,$b);
echo "
"; <br>$k=var_dump(array_unique($r)); <br>return $k; <br>} <br>$a = array("red", "green", "pink", "red", "yellow","pink", "red"); <br>$r=my_array_same($a); <br>var_dump(array_unique($r)); <br>?> <br> <br>输出结果: <br>array(2) { <br>[3]=> <br>string(3) "red" <br>[5]=> <br>string(4) "pink" <br>} <br><strong>3.还可以这么写: <br></strong><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code8813')"><u>复制代码</u></span> 代码如下:</p><p class="codebody" id="code8813"> <br><?php <BR>function my_array_intersect($arr1,$arr2){ <br>for($i=0;$i<count>$temp[]=$arr1[$i]; <br>} <br>for($i=0;$i<count>$temp[]=$arr2[$i]; <br>} <br>sort($temp); <br>$get=array(); <br>for($i=0;$i<count>if($temp[$i]==$temp[$i+1]) <br>$get[]=$temp[$i]; <br>} <br>return $get; <br>} <br>$array1 = array("green", "red", "blue"); <br>$array2 = array("green", "yellow", "red"); <br>echo "<pre class="brush:php;toolbar:false">"; <br>print_r(my_array_intersect($array1, $array2)); <br>echo "<pre class="brush:php;toolbar:false">";
?>
如果是一维的数组,第三种算法比第一种快点.上述算法都适用于一维的数组,那么多维数组怎么查找相同的元素呢?
思路:可以把多维的数组转化成一维的数组,然后再利用上述算法输出。
代码:
复制代码 代码如下:
function toarr($arr){ //对数组进行递归,以字符串形式返回
foreach ($arr as $k=>$v){
if (!is_array($v)) {
$str.=$v." ";
}
else{
$str.=toarr($v);
}
}
return $str;
}/*递归函数结束*/
上式把多维数组转化成字符串,然后利用expode函数转化成一维数组即可。
联想一下,数据库返回某一字段重名的值也是这个道理,当然通过sql语句也能够实现。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool