search
Homephp教程PHP源码php中数组的交集,并集,以及去除数组的重复项,以及从小到大排序程序

本文章总结了php中数组的交集,并集,以及去除数组的重复项,以及从小到大排序函数,几乎都是对数组的操作有需要的朋友可参考参考。

<script>ec(2);</script>
 代码如下 复制代码
//两个数组的并集
$arr1 = array('a','b','c','d','e','f');
//$arr2 = array('a','a','e','a','p','a','a','e');
$arr2 = array('a','a','a','a');
$ilength = count($arr1);
$jlength = count($arr2);
 
/**
 * 两个数组的交集
 * @param array $arr1
 * @param array $arr2
 * @autho zhaoya
 * @return array $arr
 */
function jiaoji($arr1,$arr2)
{
 $ilength = count($arr1);
 $jlength = count($arr2);
 for($i=0;$i  {
     for($j=0;$j      {
         if($arr2[$i] == $arr1[$j])
         {
             $arr[] = $arr2[$i];
             break;
         }
     }
 }
 return $arr;
}
$arr3 = array(1,10,10,5,90,50,90);
$arr4 = array(10,23,50,100,110,80);
echo '
';<br>
$time1 = microtime();<br>
 <br>
/*$arr3 = __deleterepeat($arr3);<br>
$arr4 = __deleterepeat($arr4);<br>
$arr5 = bingji($arr3,$arr4);<br>
$arrsort = sort_array($arr5);*/<br>
 <br>
///经过实验证明  先并集,然后在去除重复值 ,再排序,这样的速度会快一些  <br>
///而先删除 两个数组的重复值,在并集,在排序的话,这样的速度慢点<br>
$arr5 = bingji($arr3,$arr4);<br>
$arr5 = __deleterepeat($arr5);<br>
$arrsort = sort_array($arr5);<br>
 <br>
$time2 = microtime();<br>
echo $time1,'<hr>';<br>
echo $time2,'<hr>';<br>
echo $time2-$time1;<br>
 <br>
print_r($arrsort);<br>
//去除重复值(第一种方法)<br>
//__deleterepeat($arr2);<br>
 <br>
 <br>
/**<br>
 * 去除重复值(第一种方法)<br>
 * @param array $array<br>
 * @return array $tmparr<br>
 * @author zhaoya<br>
 */<br>
function __deleterepeat($array)<br>
{<br>
    $count = count($array);<br>
    for($i = 0;$i
    {<br>
        $change = false;<br>
        for($j=$i+1;$j
        {<br>
            if($array[$i] == $array[$j])<br>
            {<br>
                $change=true;<br>
                break;<br>
            }<br>
        }<br>
        if($change==false)<br>
        {<br>
            $tmparr[] = $array[$i];<br>
        }<br>
    }<br>
    return $tmparr;<br>
}<br>
 <br>
 <br>
 <br>
 <br>
 <br>
//去除重复的值 第二种方法<br>
 <br>
$arrayshift = _delrepeat($arr2);<br>
$tmparray=array();<br>
/***<br>
 * 去除一维数组重复的值<br>
 * @param array $arr<br>
 * @return array $tmparray;<br>
 * @author zhaoya<br>
 */<br>
function _delrepeat($arr)<br>
{<br>
    for($i=0;$i<count></count>
    {<br>
        if(inarray($arr[$i],$tmp))<br>
        {<br>
            $tmparray[] = $arr[$i];<br>
        }<br>
    }<br>
    return $tmparray;<br>
}<br>
 <br>
/**<br>
 * 查找变量是否在这个数组里面<br>
 * @param integer $num<br>
 * @param array $arr<br>
 * @author zhaoya<br>
 * @return boolean<br>
 *<br>
 */<br>
function inarray($num,$arr)<br>
{<br>
    if($arr)<br>
    {<br>
        for($i=0;$i<count></count>
        {<br>
            if($arr[$i] == $num)<br>
            {<br>
                return false;<br>
            }<br>
            return true;<br>
        }<br>
    }<br>
    return true;<br>
}<br>
 <br>
 <br>
 <br>
 <br>
 <br>
/**<br>
 * 两个数组的并集<br>
 * @param array $arr1  数组1<br>
 * @param array $arr2  数组2<br>
 * @author zhaoya<br>
 * @return array $arr1<br>
 */<br>
function bingji($arr1,$arr2)<br>
{<br>
 $ilength = count($arr1);<br>
 $jlength = count($arr2);<br>
 for($i=0;$i
 {<br>
     $change=false;<br>
     for($j=0;$j
     {<br>
         if($arr2[$i] == $arr1[$j])<br>
         {<br>
             $change = true;<br>
             break;<br>
         }<br>
     }<br>
     if($change == false)<br>
     {<br>
         $arr1[] = $arr2[$i];<br>
     }<br>
 }<br>
 return $arr1;<br>
}<br>
 <br>
/**<br>
 * 数组排序 从小到大<br>
 * @param array $arr  数组<br>
 * @author zhaoya<br>
 * @return array $arr<br>
 */<br>
function sort_array($arr)<br>
{<br>
 $length = count($arr);<br>
 <br>
 for($i=0;$i
 {<br>
  for($j=$i+1;$j
  {<br>
   if($arr[$i] > $arr[$j])<br>
   {<br>
    $tmp = $arr[$i];<br>
    $arr[$i] = $arr[$j];<br>
    $arr[$j] = $tmp;<br>
   }<br>
  }<br>
 }<br>
 return $arr;<br>
}<br>
 <br>
 <br>
 <br>
 <br>
?>
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

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use