Home  >  Article  >  Backend Development  >  Methods of merging and deduplicating one-dimensional arrays in PHP

Methods of merging and deduplicating one-dimensional arrays in PHP

不言
不言Original
2018-07-17 11:52:283181browse

This article mainly introduces the method of merging and deduplicating one-dimensional arrays in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it

Merge arrays Method

  1. array_merge:

  • Number keys, add directly to the back, key reset

  • String key, the value of the following 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))

    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);

    Related recommendations:

    php array deduplication Method reference (one-dimensional array deduplication, two-dimensional array deduplication)

    PHP two-dimensional array deduplication

    The above is the detailed content of Methods of merging and deduplicating one-dimensional 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