"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 >Backend Development >PHP Tutorial >php two array functions self-understanding php declare array php create array php array sequence
PHP array_unique() function
Remove duplicate values from the array:
<?php $a=array("a"=>"red","b"=>"green","c"=>"red"); print_r(<code>array_unique($a)</code>); ?>
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:
<?php $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=<code>array_diff($a1,$a2)</code>; 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.