How to delete duplicate elements in the PHP array

Source: Internet
Author: User
The array_unique () function sorts the values of array elements as strings. then, only the first key name is retained for each value. ignore all subsequent key names, that is, deleting repeated elements in the array, the previous article describes how to delete the header, tail, and arbitrary elements in the PHP array. This article describes how to delete repeated elements in the array using the array_unique () function.

The array_unique () function sorts the values of array elements as strings. then, only the first key name is retained for each value. ignore all subsequent key names, that is, deleting repeated elements in the array,

The syntax format is as follows:

array arry_unique(array array)

The parameter array is the input array.

The following example causes the array_unique () function to delete repeated elements in the array. the sample code is as follows:

 "; $ Result = array_unique ($ array_push); // delete the repeated elements in the array print_r ($ result); // output the deleted array?>

Output result:

The array_unique () function is only applicable to one-dimensional arrays and does not apply to multi-dimensional arrays. However, you can use array_unique () as the value in a two-dimensional array.

The following example uses the array_unique () function to delete duplicate elements in a two-dimensional array. the code is as follows :,

 $value){    $temp_array[$key] = array_unique($value);}$array = $temp_array;echo "
";print_r($array);?>

Output result:

[Recommended tutorials]

1. related topic recommendations: php Array (Array)

2. related video course recommendations: Array statistical functions: count (), array_count_values () and array_unique ()

The preceding figure shows how to delete the repeated elements in the PHP array. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.