Home >Backend Development >PHP Tutorial >PHP method to merge two tables into a new table and arrange them in order_PHP tutorial

PHP method to merge two tables into a new table and arrange them in order_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:16950browse

php method to merge two tables into a new table and arrange them in order

The specific implementation method is as follows:

The code is as follows:

/**
la (3, 5, 8, 11)
lb (2, 6, 8, 9, 11, 15)
Merge into lc and arrange in order.
Use PHP to implement it, you cannot use functions such as sort! ! ! !
**/
class union {
var $lista = array();
var $listb = array();
var $listc = array();
     
Function getlenght($arr) { //Get the table length
          return count($arr);
}
     
Function getelement($arr, $n) { //Get the nth element in the table and return
           return $e = $arr[$n] ? $arr[$n] : '';
}
     
Function listinsert($arr, $e) { //Insert element at the end of the table
          $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); //Get the table size
$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);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/922887.htmlTechArticlephp implements the method of merging two tables into a new table and arranging them in order. The specific implementation method is as follows: The code is as follows: ?php /** la (3, 5, 8, 11) lb (2, 6, 8, 9, 11, 15) are combined into lc, with...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn