Home >Backend Development >PHP Tutorial >php 归拢两个有序数组

php 归拢两个有序数组

WBOY
WBOYOriginal
2016-06-13 10:36:42806browse

php 合并两个有序数组

对于两个有序数组,每个数组都是循环一次,即可有序的排列到新的数组里面;

采取主意递增,比较,然后按顺序插入,php代码:

<?php $arr1 = array(1,2,3,4,5,6,7,8);//示例数据$arr2 = array(3,4,5,7,9,10);echo '<pre class="brush:php;toolbar:false">';print_r(mergeOrderly($arr1,$arr2));//示例function mergeOrderly($arr1,$arr2){	if(!count($arr1)){//判断参数是否有意义		return false;	}elseif(!count($arr2)){		return false;	}else {//进行合并		$arr = array();//定义最终数组容器		$arr2Num = count($arr2);//统计数组长度		$arr1Num = count($arr1);		$i1 = 0;//数组1 的循环标记		$i2 = 0;//数组2 的循环标记		while($i1  $arr2[$i2]){//需要比较数组1和数组2,小的放入目标数组,并且标记+1					$arr[] = $arr2[$i2];					$i2++;				}else{					$arr[] = $arr1[$i1];					$i1++;				}			}elseif($i1 = $arr2Num){//数组2 已经到达末尾,而数组1还为到达,情况二					$arr[] = $arr1[$i1];//直接把数组1的内容插入到目标数组中					$i1++;			}elseif($i2 = $arr1Num){//数组1已经到达末尾,而数组2还未到达,情况三					$arr[] = $arr2[$i2];//直接把数组2的内容插入到目标数组中					$i2++;			}		}		return $arr;	}}?>


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