Php multi-dimensional array de-duplication (de-duplication for any key value)-unique two-dimensional array-time complexity ~ On

Source: Internet
Author: User
: This article mainly introduces the de-duplication of php multi-dimensional arrays (de-duplication of any key value)-unique two-dimensional arrays-time complexity ~ On. For more information about PHP tutorials, see. Take a two-dimensional array as an example to deduplicate any key value. The time complexity is ~ O (n), only one foreach Loop:

 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 ,),); /* Remove any key value */function getArrayUniqueByKeys ($ arr) {$ arr_out = array (); foreach ($ arr as $ k => $ v) {$ key_out = $ v ['name']. "-". $ v ['age']; // extract the key (name age) of the internal one-dimensional array as the key of the external array if (array_key_exists ($ key_out, $ arr_out) {continue ;} else {$ arr_out [$ key_out] = $ arr [$ k]; // use key_out as the key of the external array $ arr_wish [$ k] = $ arr [$ k]; // implement two-dimensional array uniqueness} return $ arr_wish;} $ arr_wish = getArrayUniqueByKeys ($ arr); printf ("As for the arbitrarily key:
"); Print_r ($ arr_wish); echo"
";?>

Output result:

As for the arbitrarily key:
Array ([0] => Array ([name] => james [age] => 30) [1] => Array ([name] => susu [age] => 26) [new] => Array ([name] => kube [age] => 37) [list] => Array ([name] => kube [age] => 27 ))

Development instance: coupon deduplication (repeated items cannot be removed in the discount amount-order amount Field)

Requirement: only one coupon with the same discount amount and order amount is displayed for the user to choose from, and the fastest expiration coupon is displayed:

 Array ('couponcode' => '000000', 'usablestarttime' => "3033323852301056", 'usableendtime' => "1439740800", 'couponamount '=> 1440798100, 'orderamount '=> 800,), '1' => array ('couponcode' => '000000', 'usablestarttime' => "3033323852301057 ", 'usableendtime' => "1440768100", 'uponamount' => 100, 'orderamount' => 800 ,), '2' => array ('couponcode' => '201312', 'usablestarttime' => "3033323852301058", 'US AbleEndTime '=> "1440788100", 'couponamount' => 100, 'orderamount '=> 800,), '3' => array ('couponcode' => '2016 ', 'usablestarttime' => "1439740800", 'usableendtime' => "1440779100", 'couponamount '=> 200, 'orderamount' => 800 ,), '4' => array ('couponcode' => '000000', 'usablestarttime' => "3033323852301060", 'usableendtime' => "1439740800 ", 'uponamount' => 200, 'orderamount' => 800,), '5' => Rray ('couponcode' => '000000', 'usablestarttime' => "3033323852301061", 'usableendtime' => "1439740800", 'couponamount '=> 1440798100, 'orderamount '=> 800,),); // print_r ($ arrCoupon); function getArrayUniqueByKey ($ arr) {$ arrWish = array (); $ today = time (); foreach ($ arr as $ k = >$ v) {if ($ v ['usablestarttime'] <= $ today) & ($ today <= $ v ['usableendtime']) {// Determine the available date of the coupon $ keyOut = $ v ['couponamount' ]. "-". $ V ['orderamount ']; // extract the key (couponAmount orderAmount) of the internal one-dimensional array as the key of the external array if (array_key_exists ($ keyOut, $ arrWish )) {// display the first expired coupon if (intval ($ arrWish [$ keyOut] ['usableendtime'])> intval ($ v ['usableendtime']) {$ arrWish [$ keyOut] = $ v; // if the end time in the original array is large, the value will be exchanged} continue;} $ arrWish [$ keyOut] = $ v; // implement two-dimensional array uniqueness} continue;} return $ arrWish;} $ arrWant = getArrayUniqueByKey ($ arrCoupon); print_r ($ arrW Ant);?>

Output result:

Array ([100-800] => Array ([couponCode] => 3033323852301057 [usableStartTime] => 1439740800 [usableEndTime] => 1440768100 [couponAmount] => 100 [orderAmount] => 800) [200-800] => Array ([couponCode] => 3033323852301060 [usableStartTime] => 1439740800 [usableEndTime] => 1440758100 [couponAmount] => 200 [orderAmount] => 800))

The above introduces the de-duplication of php multi-dimensional arrays (de-duplication of any key value)-unique two-dimensional arrays-time complexity ~ On, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.