Home > Backend Development > PHP Tutorial > php实现两表合并成新表并且有序排列的方法_PHP

php实现两表合并成新表并且有序排列的方法_PHP

WBOY
Release: 2016-05-31 18:15:41
Original
951 people have browsed it

本文实例讲述了php实现两表合并成新表并且有序排列的方法。分享给大家供大家参考。

具体实现方法如下:

代码如下:

/**
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);
?>

希望本文所述对大家的PHP程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template