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.