PHP two tables merged into a new table and ordered

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags array class function functions get php php tutorial programming

PHP Tutorial Two tables merged into a new table and sorted in order
<?php

/**
La (3,5,8,11)
LB (2,6,8,9,11,15)
Merged into LC, arranged in order.
With PHP, you can't use functions 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-&gt;lista = Array (3, 5, 8, 11);


$listb = $phpig-&gt;listb = Array (2, 6, 8, 9, 11, 15);


$LISTC = $phpig-&gt;listc;


$lena = $phpig-&gt;getlenght ($lista); Get Table Size


$lenb = $phpig-&gt;getlenght ($LISTB);


$i = $j = 0;


while ($i &lt; $lena &amp;&amp; $j &lt; $lenb) {


$ea = $phpig-&gt;getelement ($lista, $i);


$eb = $phpig-&gt;getelement ($LISTB, $j);


if ($ea &lt;= $eb) {


$LISTC = $phpig-&gt;listinsert ($LISTC, $ea);


+ + $i;


else {


$LISTC = $phpig-&gt;listinsert ($LISTC, $eb);


+ + $j;


    }


}


while ($i &lt; $lena) {


$ea = $phpig-&gt;getelement ($lista, $i);


$LISTC = $phpig-&gt;listinsert ($LISTC, $ea);


+ + $i;


}


while ($j &lt; $lenb) {


$eb = $phpig-&gt;getelement ($LISTB, $j);


$LISTC = $phpig-&gt;listinsert ($LISTC, $eb);


+ + $j;


}

Print_r ($LISTC);
?>

Related Article

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.