Home > Backend Development > PHP Tutorial > PHP array deduplication example and analysis_PHP tutorial

PHP array deduplication example and analysis_PHP tutorial

WBOY
Release: 2016-07-13 17:17:37
Original
762 people have browsed it

Duplicates of one-dimensional array:

Just use the array_unique function. The usage example is as follows:

Copy the code The code is as follows:

                 $aa=array("apple", "banana", "pear", "apple", "wail", "watermalon");
               $bb=array_unique($aa);               ($ bb);
?>

The results are as follows: Array ( [0] => apple [1] =>banana [2] => pear [4] => wail [5]=> watermalon ).

Duplicates of two-dimensional array:

We will discuss two-dimensional arrays in two situations. One is to delete duplicates because the value of a certain key name cannot be repeated; the other is to delete duplicates because the internal one-dimensional array cannot be exactly the same, as follows Example:

㈠Because the value of a certain key name cannot be repeated, delete duplicates

Copy code The code is as follows:                                                                                                                                                                                                                                                                                        out out of > unset($arr[$k]);
}
else {
$tmp_arr[] = $v[$key];
}
}
}
sort($arr ); //sort function sorts the array
           return $arr;
$ aa = Array >'李四'),
            array('id' => 124, 'name' =>''Wang Wu'),
            array('id' => 125, 'name' = >'Zhao Liu'),
        array('id' => 126, 'name' =>''Zhao Liu')
          ); assoc_unique(&$aa, $key);
                       print_r($aa); ] =>123 [name] => Zhang San) [1] => Array ([id] => 124 [name] => Wang Wu) [2]=> Array ( [id] = > 125 [name]=> Zhao Liu) [3] => Array ( [id]=> 126 [name] => Zhao Liu) )


㈡Because the internal one-dimensional arrays cannot be exactly the same, duplicate items are deleted



Copy code

The code is as follows:


function array_unique_fb($array2D){
foreach ($array2D as $v){
$v = join( ",",$v); // For dimensionality reduction, you can also use implode to convert a one-dimensional array into a string connected with commas.
$temp[] = $v;
The duplicate string, that is, the duplicated one -dimensional array
FOREACH ($ TEMP As $ K = & GT; $ V) {
$ TEMP [$ k] = Explode (",", $ v); / /Reassemble the disassembled array
                                                                                                                                                                                                                                                                                             
$ aa = Array >'李四'),
            array('id' => 124, 'name' =>''Wang Wu'),
            array('id' => 123, 'name' = >'李四'),        array('id' => 126, 'name' =>'Zhao Liu')

                                                                                                                                   R> Print_r ($ BB)
? & Gt;


Show results: array ([0] = & gt; array ([0] = & gt; 123 [1] = & gt; Zhang San San ) [1] => Array ( [0]=> 123 [1] => Li Si) [2]=> Array ( [0] => 124 [1]=> Wang Wu) [ 4] => Array ( [0]=> 126 [1] => Zhao Liu))




http://www.bkjia.com/PHPjc/621726.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/621726.html

Duplicate items of one-dimensional array: Use the array_unique function. The usage example is as follows: Copy the code as follows: ?php $aa=array("apple","banana","pear","apple","wail","watermalon"); $...
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