简体   繁体   中英

Merge two arrays having different length based on value in php

I have two arrays with each array containing time values.I need to merge two arrays and also need to be sorted.I also dont need duplicates.I have done like this.But its not working properly.I think the issues is related to keys.

$array1= Array ( [0] => 08:00 [1] => 08:10 [2] => 08:20 [3] => 08:30
 [4] => 08:40 [5] => 08:50 [6] => 09:00 [7] => 09:10 [8] => 09:20 
 [9] => 09:30 [10] => 09:40 [11] => 09:50 [12] => 10:00  ) 


$array2 = Array ( [0] => 08:00 [1] => 08:55 [2] => 09:50 [3] => 10:45
[4] => 11:40 [5] => 12:35  ) 

 $mergedtime_array = array_unique(array_merge(($array1),$array2));

the result is coming like this:

[0]=>8:00  [1]=>8:10  [2]=>8:20 [3]=>8:30 [4]=>8:40 
[5]=>8:50 (at[6]=> 8:55 should come for my logic,
but here its [6]=>9:00) [7]=>9:10 and so on.....

please help me

尝试这个?

$mergedtime_array = array_unique(array_merge($array1,$array2));

Put sort() on the result variable like this.

$mergedtime_array = array_unique(array_merge(($array1),$array2));

mergedtime_array =sort(mergedtime_array);

It is working

$array1 = array ('0'=>'08:00','1'=>'08:10','2'=>'08:20','3'=>'08:30','4'=>'08:40', '5'=>'08:50', '6'=>'09:00', '7'=>'09:10', '8'=>'09:20','9'=>'09:30', '10'=>'09:40', '11'=>'09:50', '12'=>'10:00');

$array2 = array('0'=>'08:00','1'=>'08:55','2'=>'09:50','3'=>'10:45','4'=>'11:40','5'=>'12:35'); 

$resultArray = array_unique(array_merge($array1,$array2));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM