PHP two-dimensional array deduplication Function

Source: Internet
Author: User

PHP array RemovalRepeated itemsThere is a built-in FunctionArray_unique(), But the array_unique function of PHP is only applicable to one-dimensional arrays and does not apply to multi-dimensional arrays. The following providesTwo-dimensional arrayArray_unique Function

 // Remove duplicate values from two-dimensional arrays 
Function Array_unique_fb ( $ Array2d ) {
Foreach ( $ Array2d As $ V ) {
$ V = Join ( "," , $ V ) ; // Dimensionality reduction. You can also use implode to convert a one-dimensional array to a string connected with commas (,).
$ Temp [ ] = $ V ;
}
$ Temp = Array_unique ( $ Temp ) ; // Remove the duplicate string, that is, the duplicate one-dimensional array.
Foreach ( $ Temp As $ K => $ V ) {
$ Temp [ $ K ] = Explode ( "," , $ V ) ; // Re-assemble the split Array
}
Return $ Temp ;
}


// 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 string connected with commas (,).
$ Temp [$ K] = $ V;
}
$ Temp = array_unique ($ temp); // remove the duplicate string, that is, the duplicate one-dimensional array.
Foreach ($ temp as $ k => $ v ){
$ Array = explode (",", $ V); // re-assemble the split Array
$ Temp2 [$ K] ["ID"] = $ array [0];
$ Temp2 [$ K] ["litpic"] = $ array [1];
$ Temp2 [$ K] ["title"] = $ array [2];
$ Temp2 [$ K] ["Address"] = $ array [3];
$ Temp2 [$ K] ["starttime"] = $ array [4];
$ Temp2 [$ K] ["endtime"] = $ array [5];
$ Temp2 [$ K] ["classid"] = $ array [6];
$ Temp2 [$ K] ["ename"] = $ array [7];
}
Return $ temp2;
}

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.