Example of php array combination and deduplication

黄舟
Release: 2023-03-17 13:54:02
Original
6359 people have browsed it

In our daily development process, we cannot avoid dealing with arrays. So the most we deal with is array merging, but not only array merging but also no duplication, so today we will introduce you to php in detail An example of combining arrays and removing duplicates!

Methods to merge arrays

  1. array_merge

  • Numeric key, add directly to the back, key reset

  • String key, the value of the subsequent array will replace the previous value

  • +:

    • Numeric key, the value of the following array will not replace the previous value

    • String key, The value of the following array will replace the previous value

    Merge arrays and remove duplicates

    //1.单数组去重复
        array_unique($arrTest)//2.多数组去重复
        array_keys(array_flip($arr1)+array_flip($arr2))
    Copy after login

    Test function

            $arr1 = [1,2,3,4,5];        
            $arr2 = [1,2,3,6,7];        
            $arr3 = ['0'=>1,'1'=>2,'2'=>3,'3'=>4,'4'=>5];        
            $arr4 = ['0'=>1,'1'=>2,'2'=>3,'3'=>6,'4'=>7];        
            $arr5 = ['0'=>1,'a'=>2,'b'=>3,'c'=>4,'4'=>5];        
            $arr6 = ['0'=>1,'a'=>2,'c'=>3,'d'=>6,'4'=>7];
            dump(array_merge($arr1, $arr2));
            dump($arr1+$arr2);
            dump(array_keys(array_flip($arr1)+array_flip($arr2)));        
            echo &#39;<br>&#39;;
            dump(array_merge($arr3, $arr4));
            dump($arr3+$arr4);
            dump(array_keys(array_flip($arr3)+array_flip($arr4)));        
            echo &#39;<br>&#39;;
            dump(array_merge($arr5, $arr6));
            dump($arr5+$arr6);
    Copy after login


    Example of php array combination and deduplication

    Example of php array combination and deduplication

    Example of php array combination and deduplication

    ## Summary:

    This article uses the merging and deduplication of

    one-dimensional arrays. You can consider based on this article if it is a two-dimensional array What about mergers? This article can serve as a starting point and I hope it will be helpful to you!

    Related recommendations:

    php array merge array_merge() function usage precautions

    How to merge PHP arrays?

    Two methods of php array merging

    The above is the detailed content of Example of php array combination and deduplication. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:php.cn
    Statement of this Website
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template