PHP two-dimensional array deduplication custom function

Source: Internet
Author: User
Tags explode join

Example 1

The code is as follows: Copy code


<? Php
Function unique_array_2d ($ array2D, $ stkeep = false, $ ndformat = true)
{
// Determine whether the primary array key is retained (the primary array key can be non-numeric)
If ($ stkeep) $ stArr = array_keys ($ array2D );

// Determine whether the secondary array key is retained (all secondary array keys must be the same)
If ($ ndformat) $ ndArr = array_keys (end ($ array2D ));

// Dimensionality reduction. You can also use implode to convert a one-dimensional array to a string connected with commas (,).
Foreach ($ array2D as $ v ){
$ V = join (",", $ v );
$ Temp [] = $ v;
       }

// Remove the duplicate string, that is, the duplicate one-dimensional array.
$ Temp = array_unique ($ temp );

// Re-assemble the split array
Foreach ($ temp as $ k => $ v)
       {
If ($ stkeep) $ k = $ stArr [$ k];
If ($ ndformat)
               {
$ TempArr = explode (",", $ v );
Foreach ($ tempArr as $ ndkey => $ ndval) $ output [$ k] [$ ndArr [$ ndkey] = $ ndval;
               }
Else $ output [$ k] = explode (",", $ v );
       }

Return $ output;
}

$ Array2D = array (
'First' => array ('title' => '123', 'date' => '123 '),
'Second' => array ('title' => '123', 'date' => '123 '),
'Third' => array ('title' => '123456', 'date' => '123456 ')
);

Echo "<pre> ";
Print_r (unique_array_2d ($ array2D, true ));

/**
* Remove two-dimensional arrays by specifying the key
*
* @ Param array $ array to be duplicated
* @ Param array $ by specifies the key deduplication. If this parameter is not specified, array () is returned ()
* @ Return array
*/
Function array_multunique ($ arr, $ by = array ()){
$ Temp = array ();
Foreach ($ arr as $ key => $ val ){
Foreach ($ by as $ v ){
$ Temp [$ key]. = isset ($ val [$ v])? $ Val [$ v]: '';
               }
       }
Return array_intersect_key ($ arr, array_unique ($ temp ));
}
/* $ Aa = array (
Array ('id' => 123, 'name' => 'Zhang San '),
Array ('id' => 123, 'name' => 'Lily '),
Array ('id' => 124, 'name' => 'Wang Wu '),
Array ('id' => 125, 'name' => 'Zhao Liu '),
Array ('id' => 126, 'name' => 'Zhao Liu ')
);
$ Key = 'id ';
Array_multunique ($ aa, array ('id '));*/

Function array_remove_key ($ array, $ keys ){
If (! Is_array ($ array) |! Is_array ($ keys )){
Return false;
       }
Foreach ($ array as $ t ){
Foreach ($ keys as $ k ){
Unset ($ t [$ k]);
               }
$ Doc [] = $ t;
       }
Return $ doc;

}
/* $ Array = array (
'0' => array ('a' => 'aaaaa', 'B' => 'bbbbb ', 'C' => array ('D' => 'ddddd ', 'E' => 'eeeee ')),
'1' => array ('a' => 'aaaaa', 'B' => 'bbbbb ', 'C' => array ('D' => 'ddddd ', 'E' => 'eeeee '))
);
Print_r (array_remove_key ($ array, array ('C ')));*/

Function array_remove_key_val (& $ a, $ B, $ c ){
Foreach ($ a as $ key => $ value ){
If (isset ($ value [$ B]) & ($ value [$ B] = $ c )){
Unset ($ a [$ key]);
               }
       }
}
/* $ A = array (
Array ('id' => 1, 'num' => 10, 'type' => 'news '),
Array ('id' => 2, 'num' => 100, 'type' => 'Pic ')
);
Print_r (array_remove_key_val ($ a, "id", "1 "));*/


Example 2

 

The code is as follows: Copy code

/Remove duplicate values from two-dimensional arrays
Function array_unique_fb ($ array2D ){
Foreach ($ array2D as $ v ){
$ V = join (",", $ v); // dimension 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;
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.