Analysis of the Method of Merging Two Ordered Arrays in PHP

  • 2021-08-21 19:53:37
  • OfStack

In this paper, an example is given to illustrate the method of merging two ordered arrays in PHP. Share it for your reference, as follows:


$arr1 = array(1,2,3,4,5,6,7,8);
$arr2 = array(3,4,5,7,9,10);
// Method 1
function mergeOrderly1($arr1,$arr2){
 $i=0;$j=0;
 $int = array();
 while($i<count($arr1) && $j<count($arr2)){
  $int[] = $arr1[$i]<$arr2[$j]?$arr1[$i++]:$arr2[$j++];
 }
 while($i<count($arr1)){
  $int[] = $arr1[$i++];
 }
 while($j<count($arr2)){
  $int[] = $arr2[$j++];
 }
 //$int = array_merge($int,array_slice($arr1,$i));
 //$int = array_merge($int,array_slice($arr2,$j));
 return $int;
}
// Method 2
function mergeOrderly2($arr1,$arr2){
 $arr = array();// Define the final array container 
 $arr2Num = count($arr2);// Statistical array length 
 $arr1Num = count($arr1);
 $i1 = 0;// Array 1  Loop tag of 
 $i2 = 0;// Array 2  Loop tag of 
 while($i1 < $arr1Num || $i2 < $arr2Num){// Do you still need to merge 
  if($i1 < $arr1Num && $i2 < $arr2Num){// When neither array reaches the end, the situation 1
   if($arr1[$i1] > $arr2[$i2]){// Need to compare arrays 1 And arrays 2 The smaller ones are put into the target array and marked +1
    $arr[] = $arr2[$i2];
    $i2++;
   }else{
    $arr[] = $arr1[$i1];
    $i1++;
   }
  }elseif($i1 < $arr1Num && $i2 >= $arr2Num){// Array 2  Has reached the end, and the array 1 Also for arrival, situation 2
   $arr[] = $arr1[$i1];// Directly put the array 1 Insert the contents of into the target array 
   $i1++;
  }elseif($i2 < $arr2Num && $i1 >= $arr1Num){// Array 1 Has reached the end, and the array 2 Haven't arrived yet, the situation 3
   $arr[] = $arr2[$i2];// Directly put the array 2 Insert the contents of into the target array 
   $i2++;
  }
 }
 return $arr;
}
print_r(mergeOrderly1($arr1,$arr2));
print_r(mergeOrderly2($arr1,$arr2));

Run results:


Array
(
 [0] => 1
 [1] => 2
 [2] => 3
 [3] => 3
 [4] => 4
 [5] => 4
 [6] => 5
 [7] => 5
 [8] => 6
 [9] => 7
 [10] => 7
 [11] => 8
 [12] => 9
 [13] => 10
)
Array
(
 [0] => 1
 [1] => 2
 [2] => 3
 [3] => 3
 [4] => 4
 [5] => 4
 [6] => 5
 [7] => 5
 [8] => 6
 [9] => 7
 [10] => 7
 [11] => 8
 [12] => 9
 [13] => 10
)

Careful readers should find that the merged array is not repeated. The following is an introduction to the method of de-repeating after combining numbers under 1:

1. Single array de-duplication

array_unique($arrTest)

2. Majority group de-duplication

array_keys(array_flip($arr1)+array_flip($arr2))

Test sample:


$arr1 = array(1,2,3,4,5);
$arr2 = array(1,2,3,6,7);
$arr3 = array('0'=>1,'1'=>2,'2'=>3,'3'=>4,'4'=>5);
$arr4 = array('0'=>1,'1'=>2,'2'=>3,'3'=>6,'4'=>7);
$arr5 = array('0'=>1,'a'=>2,'b'=>3,'c'=>4,'4'=>5);
$arr6 = array('0'=>1,'a'=>2,'c'=>3,'d'=>6,'4'=>7);
var_dump(array_merge($arr1, $arr2));
var_dump($arr1+$arr2);
var_dump(array_keys(array_flip($arr1)+array_flip($arr2)));
echo '<br>';
var_dump(array_merge($arr3, $arr4));
var_dump($arr3+$arr4);
var_dump(array_keys(array_flip($arr3)+array_flip($arr4)));
echo '<br>';
var_dump(array_merge($arr5, $arr6));
var_dump($arr5+$arr6);

Run results:


array(10) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(1) [6]=> int(2) [7]=> int(3) [8]=> int(6) [9]=> int(7) } array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } array(7) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) } 
array(10) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(1) [6]=> int(2) [7]=> int(3) [8]=> int(6) [9]=> int(7) } array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } array(7) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) } 
array(8) { [0]=> int(1) ["a"]=> int(2) ["b"]=> int(3) ["c"]=> int(3) [1]=> int(5) [2]=> int(1) ["d"]=> int(6) [3]=> int(7) } array(6) { [0]=> int(1) ["a"]=> int(2) ["b"]=> int(3) ["c"]=> int(4) [4]=> int(5) ["d"]=> int(6) } 

For more readers interested in PHP related contents, please check the special topics of this site: "PHP Array (Array) Operation Skills Encyclopedia", "PHP Mathematical Operation Skills Summary", "PHP Operation and Operator Usage Summary", "php String (string) Usage Summary", "PHP Common Traversal Algorithms and Skills Summary", "PHP Data Structure and Algorithm Tutorial" and "php Programming Algorithm Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: