PHP two-dimensional array deduplication Custom Function _php Tutorial

Source: Internet
Author: User
If we are a one-dimensional data to repeat items do not say that there is a direct function, but the two-dimensional data PHP does not provide a function, the following small series to recommend two good two-dimensional array to repeat the custom function bar.

Example 1

The code is as follows Copy Code


function unique_array_2d ($array, $stkeep =false, $ndformat =true)
{
Determines whether to preserve the first-level array key (the first-level array key can be non-numeric)
if ($stkeep) $STARR = Array_keys ($array 2D);

Determines whether to preserve level two array keys (all two-level array keys must be the same)
if ($ndformat) $NDARR = Array_keys (end ($array 2D));

Dimension, you can also convert one-dimensional array to a comma-concatenated string using implode
foreach ($array 2D as $v) {
$v = Join (",", $v);
$temp [] = $v;
}

Remove duplicate strings, that is, duplicate one-dimensional arrays
$temp = Array_unique ($temp);

Re-assemble the disassembled 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;
}

$array 2D = Array (
' First ' =>array (' title ' = ' 1111 ', ' date ' = ' 2222 '),
' Second ' =>array (' title ' = ' 1111 ', ' date ' = ' 2222 '),
' Third ' =>array (' title ' = ' 2222 ', ' date ' = ' 3333 ')
);

echo "

";
Print_r (unique_array_2d ($array 2d,true));

/**
* Resets the two-dimensional array by the specified key to
*
* @param array $arr to go to the heavy arrays
* @param array $by Specify key to go to weight, the parameter does not specify that the array ()
* is returned @re Turn 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 ' = ' John Doe ') ),
Array (' id ' = = 124, ' name ' = = ' Harry '),
Array (' id ' = = ', ' name ' = ' Zhao Liu '),
Arra Y (' 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

/two-D array to remove duplicate values
function ARRAY_UNIQUE_FB ($array 2D) {
foreach ($array 2D as $v) {
$v = Join (",", $v); Dimension, you can also convert one-dimensional array to a comma-concatenated string using implode
$temp [] = $v;
}
$temp = Array_unique ($temp); Remove duplicate strings, that is, duplicate one-dimensional arrays
foreach ($temp as $k = = $v) {
$temp [$k] = Explode (",", $v); Re-assemble the disassembled array
}
return $temp;
}


Two-dimensional array removes duplicate values and preserves key values
function ARRAY_UNIQUE_FB ($array 2D) {
foreach ($array 2D as $k = + $v) {
$v = Join (",", $v); Dimension, you can also convert one-dimensional array to a comma-concatenated string using implode
$temp [$k] = $v;
}
$temp = Array_unique ($temp); Remove duplicate strings, that is, duplicate one-dimensional arrays
foreach ($temp as $k = = $v) {
$array =explode (",", $v); Re-assemble the disassembled array
$temp 2[$k ["id"] = $array [0];
$temp 2[$k] ["litpic"] = $array [1];
$temp 2[$k ["title"] = $array [2];
$temp 2[$k] [address] = $array [3];
$temp 2[$k] ["starttime"] = $array [4];
$temp 2[$k] ["endtime"] = $array [5];
$temp 2[$k] ["classid"] = $array [6];
$temp 2[$k] ["ename"] = $array [7];
}
return $temp 2;
}

http://www.bkjia.com/PHPjc/632786.html www.bkjia.com true http://www.bkjia.com/PHPjc/632786.html techarticle If we are a one-dimensional data to repeat items do not say that there is a direct function, but the two-dimensional data PHP does not provide a function, the following small series to recommend two good two-dimensional array to repeat ...

  • 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.