The method of the two-dimensional array of PHP (which preserves the key values while removing duplicates)--the uniqueness of the two-D array

Source: Internet
Author: User
Tags foreach array key printf return sort split

For the following two-dimensional array, it is required to be weighed:

$arr = Array ('
          0 ' =>array ('
                    name ' => ' James ', ' Age
                    ' =>30,
                    ),
          ' 1 ' =>array (
                    ' name ' = > ' Susu ',
                    ' age ' =>26,
                    ],
          ' 2 ' =>array (
                    ' name ' => ' James ',
                    ' age ' =>30,
                    ), '
          new ' =>array (
                    ' name ' => ' kube ',
                    ' age ' =>37,
                    ),
          ' list ' =>array (
                    ' name ' = > ' kube ',
                    ' age ' =>27,
                    )
          ;


1, the values in the one-dimensional array within the two-dimensional array cannot be exactly the same, deleting duplicates in them:

The code is as follows:


 -->Array (' name ' => ' James ', ' age ' =>30,), ' 1 ' =>arr 
                   Ay (' name ' => ' Susu ', ' age ' =>26,), ' 2 ' =>array ( 
                   ' Name ' => ' James ', ' age ' =>30,), ' New ' =>array ( 
                   ' Name ' => ' kube ', ' age ' =>37, ', ' List ' =>array (
' Name ' => ' kube ', ' age ' =>27,);   printf (before tranform the array:);
Outputs the original array print_r ($arr);
echo;   function More_array_unique ($arr =array ()) {foreach ($arr [0] as $k => $v) {$arr _inner_key[]= $k;    First, the key values of the inner array in the two-dimensional array are recorded in a one-dimensional array ($arras $k => $v) {$v =join (,, $v);      Implode () is also used to reduce the dimension $temp [$k] = $v;
    Keep the original key value $temp [] that is, do not retain the original key value} printf (after split the array:); Print_r($temp);
    The output-splitting array echo;    $temp =array_unique ($temp);   Go heavy: Remove the duplicated string foreach ($tempas $k => $v) {$a = explode (,, $v);  Split-after reorganization such as: Array ([0] => James [1] =>) $arr _after[$k]= array_combine ($arr _inner_key, $a);
Re-merge the original keys with the values}//ksort ($arr _after);//Sort as needed: Ksort the array to sort (keep the original key value key), sort to not keep the key value return$arr_after;  } $arr _new = More_array_unique ($arr);
    Call to go to the Redo function printf (Duplicate removal of the array:);
Print_r ($arr _new); echo;?>


Output results:

Before tranform the array://original arrays
Array ([0] => Array ([name] => James [age] => [1] => Array ([name] => Susu [age] =>) [2] => ; Array ([name] => James [age]=> [new] => Array ([name] => kube [age] => Notoginseng) [list] =>array ([Nam E] => kube [age] => 27))
After split the array://
Array ([0] => james,30 [1] => susu,26 [2] => james,30 [new] =>kube,37 [list] => kube,27)
Duplicate removal of TheArray://GO heavy after array
Array ([0] => Array ([name] => James [age] => [1] => Array ([name] => Susu [age] =>) [New] =& Gt Array ([name] => kube [age]=>) [list] => Array ([name] => kube [age] => 27))

2. A one-dimensional array within a two-dimensional array deletes duplicates because a key value cannot be the same:

/* For a certain key value to go to heavy * *


 
  $v)
    {
        if (In_array ($v [$key], $tmp _arr))   //Search $v[$key] is present in the $tmp_arr array, if present returns True
        {
            unset ($arr [ $k]); Destroy a variable  if the same value already exists in $tmp_arr the value
        }
        else {
            $tmp _arr[$k] = $v [$key];  Place different values in the array to save
        }
   //ksort ($arr);//ksort function to sort (keep the original key value key) sort  is not to keep the key value return
    $arr;
   }
   $key = ' name ';
   $arr _key = Second_array_unique_bykey ($arr, $key);
   printf (
As for the givenkey->%s:, $key);
   Print_r ($arr _key);
   echo 
;
? >


Output results:

As for the given Key->name:
Array ([0] => Array ([name] => James [age] => [1] => Array ([name] => Susu [age] =>) [New] =& Gt Array ([name] => kube [age]=> 37))



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.