Beginner's Questions about arrays, please provide algorithm, thank you

Source: Internet
Author: User
Rookie question, about the array, please provide the algorithm, thank you
$arrItem = Array (
Array (' Code ' =>10000, ' name ' = ' China ', ' s ' =>1),
Array (' Code ' =>30000, ' name ' = ' France ', ' s ' =>50),
Array (' Code ' =>30000, ' name ' = ' France ', ' s ' =>100),
Array (' Code ' =>10000, ' name ' = ' China ', ' s ' =>3),
Array (' Code ' =>20000, ' name ' = ' us ', ' s ' =>2),
Array (' Code ' =>30000, ' name ' = ' France ', ' s ' =>5)
);



Originally the above array, I would like to get:
$arr = Array (
Array (' Code ' =>10000, ' name ' = ' China '),
Array (' Code ' =>20000, ' name ' = ' Us '),
Array (' Code ' =>30000, ' name ' = ' France ')
);

A moment to touch the clue, ask you to provide a way, thank you!

------Solution--------------------
PHP Code
[[email protected] htdocs]$ PHP test.php
  10000, ' name ' = ' China ', ' s ' =>1 ', Array (' Code ' =>30000, ' name ' = ' France ', ' s ' =>50), Array (' Code ' =>300 XX, ' name ' = ' France ', ' s ' =>100), Array (' Code ' =>10000, ' name ' = ' China ', ' s ' =>3), Array (' Code ' =>20000         , ' name ' = ' us ', ' s ' =>2), Array (' Code ' =>30000, ' name ' = ' France ', ' s ' =>5)); foreach ($arrItem as & $elem) { unset ($elem [' s ']);} Print_r ($arrItem);? >
------Solution--------------------
PHP Code
$arrItem = Array (' Code ' =>10000, ' name ' = ' China ', ' s ' =>1), Array (' Code ' =>30000, ' name ' = = ' France ', ' s ' = &GT;50), Array (' Code ' =>30000, ' name ' = ' France ', ' s ' =>100), Array (' Code ' =>10000, ' name ' = ' China ', ' s ' =>3), Array (' Code ' =>20000, ' name ' = ' us ', ' s ' =>2), Array (' Code ' =>30000, ' name ' = ' France ', ' s ' =>5));// First delete the last element in the two-dimensional array for ($i = 0; $i < count ($arrItem), $i + +) Array_pop ($arrItem [$i]); Echo '
';//delete duplicate element $ar_tmp = Array (), foreach ($arrItem as $v) $ar _tmp[] = implode (', ', $v); $ar _tmp = Array_unique ($ar _tmp); $result = Array (), foreach ($ar _tmp as $v) $result [] = Explode (', ', $v);p Rint_r ($result);
------Solution--------------------
discuss

1. Duplicate parts take the first one
2. Remove parts of S

------Solution--------------------
PHP Code
6 floor code A bit of a problem, the wood has reserved key name, change $arritem = Array (' Code ' =>10000, ' name ' = ' China ', ' s ' =>1), Array (' Code ' =>30000, ' Name ' = = ' France ', ' s ' =>50), Array (' Code ' =>30000, ' name ' = ' France ', ' s ' =>100), Array (' Code ' =>10000, ' name ' = > ' China ', ' s ' =>3), Array (' Code ' =>20000, ' name ' = ' us ', ' s ' =>2), Array (' Code ' =>30000, ' name ' = ' France ', '    S ' =>5)), function Ar_unique ($ar) {$result = array ();    for ($i = 0; $i < count ($ar); $i + +) Array_pop ($ar [$i]);    foreach ($ar as $v) {if (!in_array ($v, $result)) $result [] = $v; } return $result;} Echo '
';p Rint_r (Ar_unique ($arrItem));/*array ([0] = = Array ([code] = 10000 [name] = = China) [1] = = Array ([code] = 30000 [name] = France) [2] = ARR AY ([code] = 20000 [name] = USA)) */
------Solution--------------------
A once and for all function.
PHP Code
$arrItem = Array (' Code ' =>10000, ' name ' = ' China ', ' s ' =>1), Array (' Code ' =>30000, ' name ' = = ' France ', ' s ' = >50), Array (' Code ' =>30000, ' name ' = ' France ', ' s ' =>100), Array (' Code ' =>10000, ' name ' = ' China ', ' s ' =>3), Array (' Code ' =>20000, ' name ' = ' us ', ' s ' =>2), Array (' Code ' =>30000, ' name ' = ' France ', ' s ' =>5));p Rint_r ( Array_group ($arrItem, ' name ', ' Code,name ')), function Array_group ($ar, $k, $fields = ' * ') {  if (! Is_array (current ($ AR)) return $ar; is a one-dimensional array that returns the original array  if (! Is_array ($fields)) {    if ($fields = = ' * ') $fields = Array_keys (current ($ar));    else $fields = explode (', ', $fields);  }  foreach ($ar as $row) {    $t = array ();    foreach ($fields as $p) $t [$p] = $row [$p];    $res [$row [$k]] = $t;  }  Return Array_values ($res);}
  • Related Article

    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.