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 ' = >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);}