php专区

 首页 > php专区 > PHP应用 > php类库 > php 两表合并成新表并且有序排列 - php类库

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

分享到:
【字体:
导读:
          ?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. /** 
  2. la (3,5,8,11) 
  3. lb(2,6,8,9,11,15) 
  4. 合并为lc,有序排列。 
  5. 用php实现,不能用sort之类的函数!!!! 
  6. **/ 
  7. class union { 
  8.     var $lista = array(); 
  9.     var $listb = array(); 
  10.     var $listc = array(); 
  11.      
  12.     function getlenght($arr) { //获得表长度 
  13.         return count($arr); 
  14.     } 
  15.      
  16.     function getelement($arr$n) { //获取表中第n个元素,返回 
  17.         return $e = $arr[$n] ? $arr[$n] : ''
  18.     } 
  19.      
  20.     function listinsert($arr$e) { //表末尾插入元素 
  21.         $arr[] = $e
  22.         return $arr
  23.     } 
  24.      
  25.    
  26. $phpig = new union(); 
  27. $lista = $phpig->lista = array(3, 5, 8, 11); 
  28. $listb = $phpig->listb = array(2, 6, 8, 9, 11, 15); 
  29. $listc = $phpig->listc; 
  30. $lena = $phpig->getlenght($lista); //取得表大小 
  31. $lenb = $phpig->getlenght($listb); 
  32. $i = $j = 0; 
  33. while($i < $lena && $j < $lenb) { 
  34.     $ea = $phpig->getelement($lista$i); 
  35.     $eb = $phpig->getelement($listb$j); 
  36.     if($ea <= $eb) { 
  37.         $listc = $phpig->listinsert($listc$ea); 
  38.         ++$i
  39.     } else { 
  40.         $listc = $phpig->listinsert($listc$eb); 
  41.         ++$j
  42.     } 
  43. while($i < $lena) { 
  44.     $ea = $phpig->getelement($lista$i); 
  45.     $listc = $phpig->listinsert($listc$ea); 
  46.     ++$i
  47. }//开源代码phpfensi.com 
  48. while($j < $lenb) { 
  49.     $eb = $phpig->getelement($listb$j); 
  50.     $listc = $phpig->listinsert($listc$eb); 
  51.     ++$j
  52. print_r($listc); 
  53. ?> 
分享到:
php查询ip所在地代码 - php类库
php查询ip所在地代码
PHPMailer邮件类 - php类库
PHPMailer邮件类 本款邮件发送功能我们是用了国外一个开源码的邮件类,大家都可能用过的PHPMailer邮件类很简单,今天来讲一下简单的使用教程,有需要的朋友可以参考下,同时像其它的附件什么的,各位朋友可以给我意见. 要注意的内容: 1,邮件的字符集设置,$mail->CharSet = "GB2312"; 这里指定字...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……