|
<? 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 "));*/ |