php 两表合并成新表并且有序排列
- <?php
-
-
-
-
-
-
- class union {
- var $lista = array();
- var $listb = array();
- var $listc = array();
-
- function getlenght($arr) {
- return count($arr);
- }
-
- function getelement($arr, $n) {
- return $e = $arr[$n] ? $arr[$n] : '';
- }
-
- function listinsert($arr, $e) {
- $arr[] = $e;
- return $arr;
- }
-
-
- }
- $phpig = new union();
- $lista = $phpig->lista = array(3, 5, 8, 11);
- $listb = $phpig->listb = array(2, 6, 8, 9, 11, 15);
- $listc = $phpig->listc;
- $lena = $phpig->getlenght($lista);
- $lenb = $phpig->getlenght($listb);
- $i = $j = 0;
- while($i < $lena && $j < $lenb) {
- $ea = $phpig->getelement($lista, $i);
- $eb = $phpig->getelement($listb, $j);
- if($ea <= $eb) {
- $listc = $phpig->listinsert($listc, $ea);
- ++$i;
- } else {
- $listc = $phpig->listinsert($listc, $eb);
- ++$j;
- }
- }
- while($i < $lena) {
- $ea = $phpig->getelement($lista, $i);
- $listc = $phpig->listinsert($listc, $ea);
- ++$i;
- }
- while($j < $lenb) {
- $eb = $phpig->getelement($listb, $j);
- $listc = $phpig->listinsert($listc, $eb);
- ++$j;
- }
- print_r($listc);
- ?>
|