Deletes the implementation code of repeated elements in the PHP array.

Source: Internet
Author: User

Deletes the implementation code of repeated elements in the PHP 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.

array_unique()Function, sorts the values of array elements as strings, and then retains only the first key name for each value. Ignoring all the key names that follow it is to delete repeated elements in the array,

The syntax format is as follows:

Array arry_unique (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:

<? Phpheader ("Content-Type: text/html; charset = UTF-8"); $ array_push = array ("PHP 文"", "www.php.cn", "www.php.cn ", "PHP from entry to entry"); // defines the array array_push ($ array_push, "Baidu", "www.php.cn"); // adds the element print_r ($ array_push ); echo "<br>"; $ result = array_unique ($ array_push); // Delete the repeated elements in the array print_r ($ result); // output the deleted array?>

Output result:

array_unique()The 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 :,

<?php$array = array(array(1,1,2),array(2,3,4,3));print_r($array);$temp_array = array();foreach($array as $key=>$value){  $temp_array[$key] = array_unique($value);}$array = $temp_array;echo "<br>";print_r($array);?>

Output result:

The preceding figure shows how to delete the details of repeated elements in the PHP array. For more information, see other related articles of helper's house!

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.