VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 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. ?> 
  58.  

出处:http://www.phpfensi.com/php/20140817/4343.html


相关教程