PHP implements one-dimensional array and two-dimensional array de-weight function

Source: Internet
Author: User
This article mainly introduces PHP implementation of a one-dimensional array and two-dimensional array de-weight function, combined with examples of PHP for a one-dimensional array and two-dimensional array traversal, judgment, sorting, weight and other related operations skills, the need for friends can refer to the next

This paper describes the implementation of PHP one-dimensional array and two-dimensional array de-weight function. Share to everyone for your reference, as follows:

The removal of duplicates in an array

Duplicate entries for one-dimensional arrays:

Using array_unique the function, use the following example:

<?php  $aa =array ("1", "2", "3", "3", "2", "Watermalon");  $BB =array_unique ($AA);  Print_r ($BB);? >

The results are as follows:

Array ([0] = 1 [1] = 2 [2] = 3 [5] = = Watermalon)

Duplicate entries for two-dimensional arrays:

For a two-dimensional array we can be discussed in one way or another because the value of one key name cannot be duplicated, and the other is because the inner one-dimensional array cannot be identical and the duplicates are deleted, the following example illustrates:

Delete duplicates because the value of a key name cannot be duplicated

<?phpfunction Assoc_unique ($arr, $key) {  $tmp _arr = Array ();  foreach ($arr as $k + $v)  {    if (In_array ($v [$key], $tmp _arr))//Search $v[$key] is present in the $tmp_arr array, if present returns True    {     unset ($arr [$k]);    }    else {     $tmp _arr[] = $v [$key];    }  }  Sort ($arr); Sort function sorts the array to  return $arr;} $AA = Array (  ' shopid ' + 1), array ('  shopid ' = + 1),  array (' shopid ' = 2),  Array (' Shopid ' + 2); $key = ' id '; $result = Assoc_unique ($aa, $key);p Rint_r ($result);? >

The results shown are:

Array ([0] = = Array ([shopid] = 1) [1] = = Array ([shopid] = 2))

The above is the whole content of this article, thank you for reading. Read more about topic.alibabacloud.com!

Related Article

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.