合并具有重叠年份范围的PHP数组值


Merge PHP Array Value that have over-lapping year ranges

如果我有一个数组,比如:

Array
(
    [0] => Array
        (
            [last_year] => 2006
            [start_year] => 2000
        )
    [1] => Array
        (
            [last_year] => 2008
            [start_year] => 2001
        )
    [2] => Array
        (
            [last_year] => 1998
            [start_year] => 1997
        )
)

有没有一种方法可以比较数组索引并合并彼此内部的last_yearstart_year?例如,上面的数组经过处理后应该是:

Array
(
    [0] => Array
        (
            [last_year] => 2008
            [start_year] => 2000
        )
    [1] => Array
        (
            [last_year] => 1998
            [start_year] => 1997
        )
)

我能想到的最好的方法是:

1)  sort this outer array by start_year.
2)  iterate over the outer array.
  a) If it overlaps with the next array if cur[last_year]>=next[start_year] Then
    set cur[last_year]=next[last_year]
  b) if it doesn't then cur = next and next is get next array.

这还没有经过测试,我已经有一段时间没有做PHP了。

// You can sort this array however you want, just sort by start_year.
$sortedArray = arr.sort();
$curIndx = 0;
for (int $i=1;$i<len($sortedArray);$i++){
   if ($sortedArray[$curIndx][last_year]>=$sortedArray[$i][start_year]){
      if (sortedArray[$i][last_year]>=sortedArray[$curIndex][last_year]){
          sortedArray[$curIndex][last_year] = sortedArray[$i][last_year];
      }
      unset(sortedArray[$i]);
   } else {
      $curIndx = $i;
   }
}

这就是2AM编码产生的结果,虽然看起来很有效(可能不是很有效)。最初的排序提示要归功于ajon。

function mergeYears($data) {
    usort($data, function($a, $b) { return $a['start_year'] > $b['start_year']; });
    do {
        $hasMerges = false;
        $size = count($data);
        $merged = [];
        for($i = 0; $i < $size - 1; $i++) {
            if ($data[$i+1]['start_year'] >= $data[$i]['start_year'] && $data[$i+1]['start_year'] < $data[$i]['last_year']) {
                if ($data[$i]['last_year'] >= $data[$i+1]) {
                    $lastYear = $data[$i]['last_year'];
                } else {
                    $lastYear = $data[$i+1]['last_year'];
                }
                $merged[] = ['start_year' => $data[$i]['start_year'], 'last_year' => $lastYear];
                $hasMerges = true;
            } else {
                $merged[] = $data[$i];
            }                    
        }
        $data = ($hasMerges) ? $merged : $data;
    } while ($hasMerges);
    return $data;
}

http://viper-7.com/y7TJH5

编辑:简化代码

function mergeYears($data) {
    usort($data, function($a, $b) { return $a['start_year'] > $b['start_year']; });
    for($i = 0; $i < count($data) - 1; $i++) {
        if ($data[$i+1]['start_year'] >= $data[$i]['start_year'] && $data[$i+1]['start_year'] < $data[$i]['last_year']) {
            $lastYearIndex = ($data[$i]['last_year'] >= $data[$i+1]['last_year']) ? $i : $i + 1;
            $merged = [
                'start_year' => $data[$i]['start_year'],
                'last_year' => $data[$lastYearIndex]['last_year']
            ];
            array_splice($data, $i, 2, [$merged]);
            $i--;
        }
    }
    return $data;
}
  1. 按起始年份排序
  2. 循环计数-1个项目
  3. 如果下一个项目的start_year>=当前索引的start_year,并且下一个项的start_年份是<比当前索引的start_year
    • 使用两个项目中较大的last_year创建两个项目的合并数组
    • 使用array_splice删除2个项目,并替换为合并的
    • 将索引向后设置1,以检查新合并的年份和下一个项目
  4. 返回新合并的年份数组