php 两表合并成新表并且有序排列
Jun 08, 2016 pm 05:26 PM
/**
la (3,5,8,11)
lb(2,6,8,9,11,15)
合并为lc,有序排列。
用php实现,不能用sort之类的函数!!!!
**/
class union {
var $lista = array();
var $listb = array();
var $listc = array();
function getlenght($arr) { //获得表长度
return count($arr);
}
function getelement($arr, $n) { //获取表中第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
$ea = $phpig->getelement($lista, $i);
$eb = $phpig->getelement($listb, $j);
if($ea
$listc = $phpig->listinsert($listc, $ea);
++$i;
} else {
$listc = $phpig->listinsert($listc, $eb);
++$j;
}
}
while($i
$ea = $phpig->getelement($lista, $i);
$listc = $phpig->listinsert($listc, $ea);
++$i;
}
while($j
$eb = $phpig->getelement($listb, $j);
$listc = $phpig->listinsert($listc, $eb);
++$j;
}
print_r($listc);
?>

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution: Your organization requires you to change your PIN

How to adjust window border settings on Windows 11: Change color and size

How to change title bar color on Windows 11?

How to enable or disable taskbar thumbnail previews on Windows 11

OOBELANGUAGE Error Problems in Windows 11/10 Repair

10 Ways to Adjust Brightness on Windows 11

How to turn off private browsing authentication for iPhone in Safari?
