欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-06-03 10:48:23
...
项目中遇到的问题。
一个大数组中有若干的小数组,但是小数组可能有重复,接口需要返回一个不重复的数组结构。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;
复制代码