Home >Backend Development >PHP Tutorial >PHP merge two ordered arrays_PHP tutorial

PHP merge two ordered arrays_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:52:58792browse

For two ordered arrays, each array is looped once and can be arranged into a new array in an orderly manner;
Take the idea of ​​incrementing, comparing and then inserting in order, php code:
[php]
$arr1 = array(1,2,3,4,5,6,7,8);//Sample data
$arr2 = array(3,4,5,7,9,10);
echo '

'; 
print_r(mergeOrderly($arr1,$arr2));//Example
function mergeOrderly($arr1,$arr2){
If(!count($arr1)){//Determine whether the parameters are meaningful
        return false;
}elseif(!count($arr2)){
        return false;
}else {//Merge
          $arr = array();//Define the final array container
          $arr2Num = count($arr2);//Statistical array length
          $arr1Num = count($arr1);
          $i1 = 0;//Loop mark of array 1
          $i2 = 0;//Loop mark of array 2
​​​​while($i1 < $arr1Num || $i2 < $arr2Num){//Do you still need to merge?
If($i1 < $arr1Num && $i2 < $arr2Num){//When neither array reaches the end, case 1
If($arr1[$i1] > $arr2[$i2]){//Need to compare array 1 and array 2, put the smaller one into the target array, and mark +1
                            $arr[] = $arr2[$i2];
                         $i2++;
                                                                                                                                                                                            $arr[] = $arr1[$i1];
                         $i1++;
                                                                                                                                          }elseif($i1 < $arr1Num && $i2 >= $arr2Num){//Array 2 has reached the end, but array 1 has not yet arrived, case two
                                                                                                                                                                                                                                          $arr[] = $arr1[$i1];//Directly insert the contents of array 1 into the target array
                         $i1++;
                  }elseif($i2 < $arr2Num && $i1 >= $arr1Num){//Array 1 has reached the end, but array 2 has not yet arrived, case three
                                 $arr[] = $arr2[$i2];//Insert the contents of array 2 directly into the target array
                         $i2++;
                                                                                                                                               } 
         return $arr;
}  
}
?>

Author: dats0407


http://www.bkjia.com/PHPjc/478058.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478058.htmlTechArticleFor two ordered arrays, each array is looped once to arrange them into new ones in order Inside the array; take the idea of ​​incrementing, comparing, and then inserting in order, php code: [php] ?p...
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