0

如果我有一个像这样的数组:

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_year它们start_year?比如上面的数组经过处理后应该是:

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

    [1] => Array
        (
            [last_year] => 1998
            [start_year] => 1997
        )
)
4

2 回答 2

1

我能想到的最好方法是:

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;
   }
}
于 2012-08-31T08:16:08.867 回答
0

这就是 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. 首先按 start_year 排序
  2. 循环计数 - 1 个项目
  3. 如果下一项的 start_year >= 当前索引的 start_year 并且下一项的 start_year < 比当前索引的 start_year
    • 使用两者中较大的 last_year 创建这两个项目的合并数组
    • 使用 array_splice 删除 2 项并替换为合并的
    • 将索引设置回 1 以检查新合并年份与下一项
  4. 返回新合并的年份数组
于 2012-08-31T08:56:43.030 回答