从PHP数组求差想到的

 

版本一:

    

        

            

        

    

            

function array_diff($array_1, $array_2) {
                $diff = array();
            
                foreach ($array_1 as $k => $v1) {
                    $flag = false;
                    foreach ($array_2 as $v2) {
                        if ($flag = ($v1 == $v2)) {
                            break;
                        }
                    }
            
                    if (!$flag) {
                        $diff[$k] = $v1;
                    }
                }
            
                return $diff;
            }
            

            

 

版本 二

    

        

            

        

    

            

function array_diff($array_1, $array_2) {
                foreach ($array_1 as $key => $item) {
                    if (in_array($item, $array_2, true)) {
                        unset($array_1[$key]);
                    }
                }
            
                return $array_1;
            }

            

版本 三

    

        

            

        

    

            

function array_diff($array_1, $array_2) {
                $array_2 = array_flip($array_2);
                foreach ($array_1 as $key => $item) {
                    if (isset($array_2[$item])) {
                        unset($array_1[$key]);
                    }
                 }
            
                return $array_1;
            }
            

            

 

 

最后,让我们比较一下isset 与array_key_exists 的效率吧:

查找一个存在的key

    

        

            

        

    

            

//isset 与 array_key_exitsts的比较
            
            for($i = 0 ;$i< 100000;$i++){
                $arr['a'.$i] = 'A'.$i;
            }
            
            $time = microtime(1);
            $a = isset($arr['a50000']);
            $t1 = microtime(1) - $time;
            
            $time = microtime(1);
            $a = array_key_exists('a50000',$arr);
            $t2 = microtime(1) - $time;
            
            echo $t1;
            echo "\n";
            echo $t2;
            
            
            exit;

            

输出:

 ———-PHP代码调试 ———-
4.10079956055E-005
1.4066696167E-005
输出完成 (耗时 5 秒) – 正常终止

 

查找一个不存在的:

    

        

            

        

    

            

//isset 与 array_key_exitsts的比较
            
            for($i = 0 ;$i< 100000;$i++){
                $arr['a'.$i] = 'A'.$i;
            }
            
            $time = microtime(1);
            $a = isset($arr['a50000000000']);
            $t1 = microtime(1) - $time;
            
            $time = microtime(1);
            $a = array_key_exists('a50000000000',$arr);
            $t2 = microtime(1) - $time;
            
            echo $t1;
            echo "\n";
            echo $t2;
            
            
            exit;

            

输出:

 ———-PHP代码调试 ———-
4.31537628174E-005
1.19209289551E-005
输出完成 (耗时 0 秒) – 正常终止

 

结论:数组中检查一个key是否存在时,用array_key_exists 比 isset 要快的多,特别是出来的数组很大时,至于个中缘由,有待进一步研究

留下评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据