PHP to implement two tables merged into a new table and orderly arrangement of methods _php skills

Source: Internet
Author: User

The example of this article describes the implementation of the two tables in PHP to merge into a new table and orderly arrangement of the method. Share to everyone for your reference.

The implementation methods are as follows:

Copy Code code as follows:
<?php
/**
La (3,5,8,11)
LB (2,6,8,9,11,15)
Merged into LC, arranged in order.
In PHP, you can't use a function like sort!!!!
**/
Class Union {
var $lista = array ();
var $listb = array ();
var $LISTC = array ();

function Getlenght ($arr) {//Get table length
return count ($arr);
}

function GetElement ($arr, $n) {//Get the nth element in the table, return
return $e = $arr [$n]? $arr [$n]: ';
}

function Listinsert ($arr, $e) {//end of Table Insert element
$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 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);
?>

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.