PHP two-dimensional array deduplication instance analysis, two-dimensional array instance analysis

Source: Internet
Author: User

PHP two-dimensional array deduplication instance analysis, two-dimensional array instance analysis

This article analyzes the PHP two-dimensional array deduplication method. We will share this with you for your reference. The details are as follows:

We all know that the one-dimensional array is used to reuse the system function array_unique ($ arr)

Then today I used a two-dimensional array, and I also want to remove duplicates. Baidu has a lot of data, which means converting two-dimensional data to one-dimensional data and then using array_unique ($ arr)

I was so angry that I decided to write one by myself. Easier to understand than him. record it for later use.

Header ('content-type: text/html; charset = utf8'); $ arr = array ('id' => 1, 'psid '=> 'p101 ', 'fullname' => 'Course Plan full name 101', 'userid' => 4), array ('id' => 1, 'psid' => 'p101 ', 'fullname' => 'Course Plan full name 101', 'userid' => 3), array ('id' => 1, 'psid' => 'p101 ', 'fullname' => 'Course Plan full name 101', 'userid' => 3), array ('id' => 1, 'psid' => 'p101 ', 'fullname' => 'Course Plan full name 101', 'userid' => 2), array ('id' => 2, 'psid '=> 'p102 ', 'fullname' => 'new course scheduler ', 'userid' => 4), array ('id' => 2, 'psid' => 'p102 ', 'fullname' => 'new course scheduler', 'userid' => 3), array ('id' => 2, 'psid' => 'p102 ', 'fullname' => 'new course scheduler', 'userid' => 3), array ('id' => 2, 'psid '=> 'p102', 'fullname' => 'new course scheduler ', 'userid' => 2); $ arr = er_array_unique ($ arr ); foreach ($ arr as $ v) {echo 'id :'. $ v ['id']. 'psid :'. $ v ['psid ']. 'fullname :'. $ v ['fullname']. 'userid :'. $ v ['userid']. '<br/>';} // two-dimensional array simple deduplication function er_array_unique ($ arr) {$ newarr = array (); if (is_arra Y ($ arr) {foreach ($ arr as $ v) {if (! In_array ($ v, $ newarr, true) {$ newarr [] = $ v ;}} else {return false;} return $ newarr ;}

Print result:

Id: 1 psid: P101 fullname: course plan full name 101 userid: 4id: 1 psid: P101 fullname: course plan full name 101 userid: 3id: 1 psid: P101 fullname: course Plan full name 101 userid: 2id: 2 psid: P102 fullname: New Course Plan userid: 4id: 2 psid: P102 fullname: New Course Plan userid: 3id: 2 psid: P102 fullname: new course plan userid: 2

Note: in_array ($ need, $ arr, $ strict) in this method)

Before PHP version 4.2.0, $ need cannot be an array. If $ stric is true, the type to be searched in $ need and $ arr is strictly matched.

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.