Heim  >  Artikel  >  Backend-Entwicklung  >  求此算法的优化版!!!!

求此算法的优化版!!!!

WBOY
WBOYOriginal
2016-07-25 08:48:45769Durchsuche
项目中遇到的问题。
一个大数组中有若干的小数组,但是小数组可能有重复,接口需要返回一个不重复的数组结构。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;
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:excelTime 用于excel 导入日期格式 Nächster Artikel:php 创建sprite