求此演算法的最佳化版!!!!

來源:互聯網
上載者:User
項目中遇到的問題。
一個大數組中有若干的小數組,但是小數組可能有重複,介面需要返回一個不重複的數組結構。PHP中的內建function array_merge()試過,不可以實現對小數組的排序。下面是我使用的方式。我只能說效率很低,如果你看到這篇文章,有好的方法,請告訴我。:)
//$hotel_setting可能含有重複的大數組。
小數組結構為array('hotel_adt_cnt'=>'','hotel_chd_cnt'=>'');
  1. $hotel_unique = array();
  2. while ( 1 ) {
  3. $cmp = array_shift( $hotel_setting );
  4. $repeat = false;
  5. foreach ( $hotel_setting as $val ) {
  6. if ( $val['hotel_adt_cnt'] == $cmp['hotel_adt_cnt'] && $val['hotel_chd_cnt'] == $cmp['hotel_chd_cnt']) {
  7. $repeat = true;
  8. break;
  9. }
  10. }
  11. if(!$repeat)
  12. {
  13. $hotel_unique[] = $cmp;
  14. }
  15. if ( count( $hotel_setting ) == 0 ) {
  16. break;
  17. }
  18. }
  19. $hotel_setting = $hotel_unique;
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.