Rumah >pembangunan bahagian belakang >tutorial php >PHP输出数组中重名的元素的几种处理方法_PHP
"; <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><u>复制代码</u> 代码如下: <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">";