For more information about arrays, please provide algorithms. thank you.

Source: Internet
Author: User
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 );}

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.