Cainiao's Question: Please provide algorithms for arrays. Thank you & lt ;? Php $ arrItemarray (Array (code & gt; 10000, name & gt; China, s & gt; 1), Array (code & gt; 30000, name & gt; France, s & gt; 50), Array (code & gt; 30000, name Cainiao 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' => 'u.s. ', 's' => 2 ),
Array ('code' => 30000, 'name' => 'France ', 's' => 5)
);
It was originally the above array, I want to get:
$ Arr = array (
Array ('code' => 10000, 'name' => 'China '),
Array ('code' => 20000, 'name' => 'u.s '),
Array ('code' => 30000, 'name' => 'France ')
);
I couldn't figure it out for a moment. could you please help me? thank you!
------ Solution --------------------
PHP code
[Work@cq01-client-test03.vm.baidu.com htdocs] $ php test. php
10000, 'name' => 'China', 's' => 1), Array ('code' => 30000, 'name' => 'French ','s '=> 50), Array ('code' => 30000, 'name' => 'France', 's' => 100 ), array ('code' => 10000, 'name' => 'China', 's' => 3), Array ('code' => 20000, 'name' => 'u.s. ', 's' => 2), Array ('code' => 30000, 'name' => 'French ','s '=> 5); foreach ($ arrItem as & $ elem) {unset ($ elem ['s']);} print_r ($ arrItem);?>
------ Solution --------------------
PHP code
$ ArrItem = array (Array ('code' => 10000, 'name' => 'China', 's' => 1), Array ('code' => 30000, 'name' => 'French ', 's' => 50), Array ('code' => 30000, 'name' => 'French ', 's' => 100), Array ('code' => 10000, 'name' => 'China', 's' => 3 ), array ('code' => 20000, 'name' => 'u.s. ', 's' => 2), Array ('code' => 30000, 'name' => 'French ', 's' => 5); // delete the last element in the two-dimensional array for ($ I = 0; $ I <count ($ arrItem); $ I ++) array_pop ($ arrItem [$ I]); echo''; // Delete repeated elements $ 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 ); print_r ($ result );
------ Solution --------------------
Discussion
1. repeat the first part
2. remove the s part.
------ Solution --------------------
PHP code
// There is a problem with the code on the 6th floor. if the wood has a reserved key name, change $ 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' => 'u.s ','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]); foreac H ($ ar as $ v) {if (! In_array ($ v, $ result) $ result [] = $ v;} return $ result;} echo''; Print_r (ar_unique ($ arrItem);/* Array ([0] => Array ([code] => 10000 [name] => China) [1] => Array ([code] => 30000 [name] => France) [2] => Array ([code] => 20000 [name] => USA ))*/
------ Solution --------------------
Come to a function once and for all
PHP code
$ ArrItem = array (Array ('code' => 10000, 'name' => 'China', 's' => 1), Array ('code' => 30000, 'name' => 'French ', 's' => 50), Array ('code' => 30000, 'name' => 'French ', 's' => 100), Array ('code' => 10000, 'name' => 'China', 's' => 3 ), array ('code' => 20000, 'name' => 'u.s. ', 's' => 2), Array ('code' => 30000, 'name' => 'French ', 's' => 5); print_r (array_group ($ arrItem, 'name', 'Code, name ')); function array_group ($ ar, $ k, $ fields = '*') {if (! Is_array (current ($ ar) return $ ar; // 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 );}