Home > Backend Development > PHP Tutorial > 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
Release: 2016-07-29 08:54:36
Original
1071 people have browsed it

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>);
?>
Copy after login

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);
?>
Copy after login

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.

Related labels:
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