doufu9947 2014-09-11 18:06
浏览 26
已采纳

PHP合并两个数组与范围

I have two arrays :

$a = array(
array("begin"=>0, "end"=>200,"value"=>90),
array("begin"=>200, "end"=>600,"value"=>50),
array("begin"=>600, "end"=>1000,"value"=>90)
);

$b = array(
array("begin"=>-5, "end"=>50,"value"=>10),
array("begin"=>550, "end"=>590,"value"=>30)
);

Target, merging two arrays with the following criteria:

  1. merging the array $b into $a (called $result)
  2. if item in $b overlap with any item in array $a, it should cut the interval in $a
  3. for any two intervals with same "begin" & "end" after 2), it should choose the smaller value

The answer i would expected:

$result = array(
    array("begin"=>-5, "end"=>50,"value"=>10),
    array("begin"=>50, "end"=>200,"value"=>90),
    array("begin"=>200, "end"=>550,"value"=>50),
    array("begin"=>550, "end"=>590,"value"=>30),
    array("begin"=>590, "end"=>600,"value"=>50),
    array("begin"=>600, "end"=>1000,"value"=>90)
    );

What would be the best way to accomplish this? Thank you everyone.

  • 写回答

2条回答 默认 最新

  • dpa0760 2014-09-13 04:36
    关注

    Thanks everyone idea. I just come up with the code and post it here in case if someone have the same problem, they could make reference to.

    I first combine the $a and $b to a single array $tmp_combine and sort them according to 'begin'. Construct an unique sorted index array $ind containing all 'start' and 'end' of the $tmp_combine. Then calculate the min value for each index point pair from $tmp_combine, construct an array $ind_SR.

    for number of item in $ind_SR, do the merge if they have the same value consecutively and output the final answer to $result.

     $tmp_combine = array_orderby(array_merge($a,$b), 'begin', SORT_ASC);
    
           $ind = array_merge(array_column($tmp_combine, 'begin'),array_column($tmp_combine, 'end'));
    
           $ind =  array_unique($ind);
           asort($ind);
           $ind = array_values($ind);
    
           $index = 0;
           $ch_now = $ind[0];
    
    
          for ($i=0; $i<count($ind)-1; $i++){ //calculate the mid pt min value
             $mid_pt = ($ind[$i] + $ind[$i+1]) /2;
             array_push($ind_SR, find_min_SR($tmp_combine,$mid_pt));
            }
    
          for ($i=0; $i<count($ind_SR); $i++){
    
             if ($ch_now <= $ind[$i]){
             $start = $ind[$i];
             $end = $ind[$i+1];
             $result[$index]['begin'] = $ind[$i];
    
             for ($j=$i; $j<count($ind_SR); $j++){
                $start_j = $ind[$j];
                $end_j = $ind[$j+1];
    
                if ($ind_SR[$j] == $ind_SR[$i]){
                    $result[$index]['begin'] = $start;
                   $result[$index]['end']  = $end_j ;
                   $result[$index]['value'] =  $ind_SR[$i];
                    $ch_now = $ind[$j+1];
                } else{
                    $result[$index]['end']  = $ind[$j] ;
                    break; break;
                }
    
             }
    
    
            $index++;  
    
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站