Home  >  Article  >  Backend Development  >  Summary of php implementation of array arrangement and ordered arrangement methods

Summary of php implementation of array arrangement and ordered arrangement methods

伊谢尔伦
伊谢尔伦Original
2017-07-17 10:57:301467browse

Using array_merge() function

used a array_unique to remove duplicates in a array, but found that the subscript was retained The subscript of the original array, but PHP needs to use for loopThe subscripts need to be neat, so looking for a way to rearrange the array subscripts array_merge can solve this problem

array_merge() function combines two or more arrays are combined into one array.

If the key name is repeated, the key value of the key will be the value corresponding to the last key name (the later one will overwrite the previous one). If the array is numerically indexed, the key names are re-indexed in a consecutive fashion.

Note: If only an array is input to the array_merge() function, and the key name is an integer, the function will return a new array with an integer key name, whose key name starts with 0 starts reindexing.

<?php
$a1=array("a"=>"Horse","b"=>"Dog");
$a2=array("c"=>"Cow","b"=>"Cat");
print_r(array_merge($a1,$a2));
?>

Output:

Array ([a] => Horse [b] => Cat [c] => Cow )

Example 2

Use only one array parameter:

<?php
$a=array(3=>"Horse",4=>"Dog");
print_r(array_merge($a));
?>

Output:

Array ( [0] => Horse [1] => Dog )

Merge into new Method of table and orderly arrangement

<?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] : &#39;&#39;; 
    } 
     
    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 < $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); 
?>

The above is the detailed content of Summary of php implementation of array arrangement and ordered arrangement methods. For more information, please follow other related articles on the PHP Chinese website!

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