"green", "c"=>"red"); print_r(array_unique($a)); The ?>array_unique() function removes duplicates from an array and returns the resulting array. When several array elements are equal, only the first element is retained and the other elements are deleted. The key names in the returned array remain unchanged. Note: The retained array will retain the"/> "green", "c"=>"red"); print_r(array_unique($a)); The ?>array_unique() function removes duplicates from an array and returns the resulting array. When several array elements are equal, only the first element is retained and the other elements are deleted. The key names in the returned array remain unchanged. Note: The retained array will retain the">

Home>Article>Backend Development> php two array functions self-understanding php declare array php create array php array sequence

php two array functions self-understanding php declare array php create array php array sequence

WBOY
WBOY Original
2016-07-29 08:54:36 979browse

PHP array_unique() function

Remove duplicate values from the array:

"red","b"=>"green","c"=>"red"); print_r(array_unique($a)); ?>

array_unique() function removes duplicate values from the array and returns the result array.

When the values of several array elements are equal, only the first element is retained and the other elements are deleted.

The key names in the returned array remain unchanged.

Note:The retained array will retain the key type of the first array item.

array_unique() first sorts the values as strings, then only retains the first encountered key for each value, and then ignores all subsequent keys. This does not mean that the first occurrence of the same value in an unsorted array will be preserved.

PHP array_diff() function

compares the key values of two arrays and returns the difference:

"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=array_diff($a1,$a2); print_r($result); ?>

array_diff() function returns the difference array of two arrays. This array contains all keys that are in the array being compared, but are not in any of the other argument arrays.

In the returned array, the key names remain unchanged.

One or any number of arrays can be compared with the first array.

The above introduces the self-understanding of two array functions in PHP, including array functions and PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.

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