Home >Backend Development >PHP Problem >How to remove duplicates from two arrays in php

How to remove duplicates from two arrays in php

藏色散人
藏色散人Original
2021-03-29 09:50:472747browse

php method to remove duplicate values ​​from two arrays: first create a PHP sample file; then delete duplicate values ​​through the "rray_diff($f,$e); array_diff($e,$f);" method Can.

How to remove duplicates from two arrays in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

Use array_diff to remove duplicate values ​​from two arrays

Business scenario, get two strings $e and $f.

correspond to the previous data and the replaced data respectively, we will not change the duplicates

Delete the previous one and add the new one

The commented ones are typed out by normal logic, but I really don’t want to use a loop. It’s better to write the built-in function in low-level C and run faster

$e = 22,33,44;
$f = 22,31,54;
        $e = explode(',',$e);
        $f = explode(',',$f);
        //删除重复值
//        foreach ($f as $k1=>$v1){
//            foreach ($e as $k2=>$v2){
//                if($v1===$v2){
//                    unset($f[$k1]);unset($e[$k2]);
//                }
//            }
//        }
        $f1=array_diff($f,$e);
        $e1=array_diff($e,$f);
        print_r($e1);//33,44
print_r($f1);//31,54

【Recommended learning: PHP video tutorial

The above is the detailed content of How to remove duplicates from two arrays in php. For more information, please follow other related articles on the PHP Chinese website!

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