网站地图    收藏   

主页 > php专栏 > php类库 >

php 两表合并成新表并且有序排列 - php类库

来源:自学PHP网    时间:2014-11-30 12:53 作者: 阅读:次

[导读] ?php/**la(3,5,8,11)lb(2,6,8,9,11,15)合并为lc,有序排列。用php实现,不能用sort之类的函数!!!!**/classunion{var$lista=array();var$listb=array();var$listc=array();functio......

php 两表合并成新表并且有序排列

  1. <?php 
  2. /** 
  3. la (3,5,8,11) 
  4. lb(2,6,8,9,11,15) 
  5. 合并为lc,有序排列。 
  6. 用php实现,不能用sort之类的函数!!!! 
  7. **/ 
  8. class union { 
  9.     var $lista = array(); 
  10.     var $listb = array(); 
  11.     var $listc = array(); 
  12.      
  13.     function getlenght($arr) { //获得表长度 
  14.         return count($arr); 
  15.     } 
  16.      
  17.     function getelement($arr, $n) { //获取表中第n个元素,返回 
  18.         return $e = $arr[$n] ? $arr[$n] : ''; 
  19.     } 
  20.      
  21.     function listinsert($arr, $e) { //表末尾插入元素 
  22.         $arr[] = $e; 
  23.         return $arr; 
  24.     } 
  25.      
  26.    
  27. } 
  28. $phpig = new union(); 
  29. $lista = $phpig->lista = array(3, 5, 8, 11); 
  30. $listb = $phpig->listb = array(2, 6, 8, 9, 11, 15); 
  31. $listc = $phpig->listc; 
  32. $lena = $phpig->getlenght($lista); //取得表大小 
  33. $lenb = $phpig->getlenght($listb); 
  34. $i = $j = 0; 
  35. while($i < $lena && $j < $lenb) { 
  36.     $ea = $phpig->getelement($lista, $i); 
  37.     $eb = $phpig->getelement($listb, $j); 
  38.     if($ea <= $eb) { 
  39.         $listc = $phpig->listinsert($listc, $ea); 
  40.         ++$i; 
  41.     } else { 
  42.         $listc = $phpig->listinsert($listc, $eb); 
  43.         ++$j; 
  44.     } 
  45. } 
  46. while($i < $lena) { 
  47.     $ea = $phpig->getelement($lista, $i); 
  48.     $listc = $phpig->listinsert($listc, $ea); 
  49.     ++$i; 
  50. }//开源代码phpfensi.com 
  51. while($j < $lenb) { 
  52.     $eb = $phpig->getelement($listb, $j); 
  53.     $listc = $phpig->listinsert($listc, $eb); 
  54.     ++$j; 
  55. } 
  56. print_r($listc); 
  57. ?> 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论