This article mainly introduces PHP implementation of a one-dimensional array and two-dimensional array de-weight function, combined with examples of PHP for a one-dimensional array and two-dimensional array traversal, judgment, sorting, weight and other related operations skills, the need for friends can refer to the next
This paper describes the implementation of PHP one-dimensional array and two-dimensional array de-weight function. Share to everyone for your reference, as follows:
The removal of duplicates in an array
Duplicate entries for one-dimensional arrays:
Using array_unique
the function, use the following example:
<?php $aa =array ("1", "2", "3", "3", "2", "Watermalon"); $BB =array_unique ($AA); Print_r ($BB);? >
The results are as follows:
Array ([0] = 1 [1] = 2 [2] = 3 [5] = = Watermalon)
Duplicate entries for two-dimensional arrays:
For a two-dimensional array we can be discussed in one way or another because the value of one key name cannot be duplicated, and the other is because the inner one-dimensional array cannot be identical and the duplicates are deleted, the following example illustrates:
Delete duplicates because the value of a key name cannot be duplicated
<?phpfunction Assoc_unique ($arr, $key) { $tmp _arr = Array (); foreach ($arr as $k + $v) { if (In_array ($v [$key], $tmp _arr))//Search $v[$key] is present in the $tmp_arr array, if present returns True { unset ($arr [$k]); } else { $tmp _arr[] = $v [$key]; } } Sort ($arr); Sort function sorts the array to return $arr;} $AA = Array ( ' shopid ' + 1), array (' shopid ' = + 1), array (' shopid ' = 2), Array (' Shopid ' + 2); $key = ' id '; $result = Assoc_unique ($aa, $key);p Rint_r ($result);? >
The results shown are:
Array ([0] = = Array ([shopid] = 1) [1] = = Array ([shopid] = 2))
The above is the whole content of this article, thank you for reading. Read more about topic.alibabacloud.com!