Two methods for removing duplicate items from two-dimensional arrays in php

Source: Internet
Author: User
This article mainly introduces two methods for removing duplicate items from two-dimensional arrays in php. you can compare them to see which one is more suitable for you, for more information about how to remove the duplicate values of two-dimensional arrays in php, see the following code:
Method 1:

// Remove the duplicate value from the two-dimensional array function array_unique_fb ($ array2D) {foreach ($ array2D as $ v) {$ v = join (',', $ v ); // reduce the dimension. you can also use implode to convert a one-dimensional array to a comma-connected string $ temp [] = $ v ;}$ temp = array_unique ($ temp ); // remove the duplicate string, that is, the repeated one-dimensional array foreach ($ temp as $ k => $ v) {$ temp [$ k] = explode (',', $ v); // re-assemble the split array} return $ temp ;}

Method 2:

// Remove duplicate values from the two-dimensional array and retain the key value function array_unique_fb ($ array2D) {foreach ($ array2D as $ k = >$ v) {$ v = join (', ', $ v); // dimension reduction. you can also use implode to convert a one-dimensional array to a comma-connected string $ temp [$ k] = $ v ;} $ temp = array_unique ($ temp); // remove the duplicate string, that is, the repeated one-dimensional array foreach ($ temp as $ k => $ v) {$ array = explode (',', $ v ); // re-assemble the split array // modify the following indexes as needed. $ temp2 [$ k] ['id'] = $ array [0]; $ temp2 [$ k] ['title'] = $ array [1]; $ temp2 [$ k] ['keyword'] = $ array [2]; $ temp2 [$ k] ['content'] = $ array [3];} return $ temp2 ;}

The two php methods to remove duplicate items of two-dimensional arrays have their own advantages and disadvantages. you can choose one based on your situation.

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.