4

これを変更する方法:

配列
((
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 11
    [8] => 21
    [9] => 22
    [10] => 23
    [11] => 24
)。

これに:

1-7、11、21-24

PHP配列にこのような数値のリストがありますが、このリストを少し小さくしたいと思います。

2000:3 6 7 11 15 17 25 36 42 43 45
2001:2 3 4 5 6 9 10 11 12 13 34 37 45 46 47 48 49 50 51 52
2002:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 33 34 35 36 37 39 40 41 42 43 44 45 46 47 48 49 50 51 52
2003:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
2004:1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 18 19 21 22 23 24 25 26 27 28 29 30 31 32 33 34 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
2005:1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
2006:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
2007:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
4

5 に答える 5

6

面白い仕事。

これがまさにあなたが望むことをするデモスクリプトです。

味に微調整します。

コード

<?php

$groups = array();
$active_group = 0;

$output = array();
$output_counter = 0;

$nums = array( 1, 2, 3, 4, 5, 6, 7, 11, 21, 22, 23, 24 );


foreach( $nums as $k => $num ) {

    // if this isn't the first item, and the current number
    // isn't one more than the previous one, increment the counter
    if( $k !== 0 && $nums[$k] !== $nums[$k-1]+1 )
        $active_group ++;

    // add this number to a group
    $groups[ $active_group ][] = $num;

}


// take the 1st and last of each group
foreach( $groups as $group ) {

    $first = array_shift( array_values($group) );
    $output[$output_counter][] = $first;

    $last = array_pop( array_values($group) );
    if( $first !== $last )
        $output[$output_counter][] = $last;

    $output_counter++;

}


echo '<pre>';
print_r($output);

?>

出力

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 7
        )

    [1] => Array
        (
            [0] => 11
        )

    [2] => Array
        (
            [0] => 21
            [1] => 24
        )

)
于 2012-08-09T00:26:07.170 に答える
1

単一のループで十分です。範囲データを格納するために、「前の」反復の値と「開始」の値を追跡する必要があります。

コード:

$prev = -1;                                                    // initialize out of range
foreach ($numbers as $n) {
    if (!isset($start)) {                                      // first iteration
        $start = $n;                                           // declare $start
    } elseif ($n != $prev + 1) {                               // not consecutive
        $result[] = $start == $prev ? $prev : "$start-$prev";  // store single or ranged values
        $start = $n;                                           // update $start
    }
    $prev = $n;                                                // declare / update $prev
}
$result[] = $start == $prev ? $prev : $start . '-' . $prev;    // store final iteration data
echo implode(', ', $result);                                   // comma delimit the values

からの出力$numbers = [1, 2, 3, 4, 5, 6, 7, 11, 21, 22, 23, 24];:(デモ

1-7, 11, 21-24

からの出力$numbers = [1, 3, 5, 6, 11, 21, 22, 23, 24, 26];:(デモ

1, 3, 5-6, 11, 21-24, 26
于 2018-11-03T16:01:06.607 に答える
0

これは、整数の配列を必要な文字列形式に圧縮する方法と、その文字列形式を整数の配列に拡張する方法の両方です。

function compress($expanded) {
    $low = -1;
    $prevNum = -1;
    $expanded = array_unique($expanded);
    sort($expanded, SORT_NUMERIC);
    foreach($expanded as $num) {
        if($low == -1) {
            $low = $num;
        } else if($num - $prevNum > 1) { 
            $compact[] = ($prevNum - $low >= 1) ? sprintf("%d-%d", $low, $prevNum) : $prevNum; 
            $low = $num;
        } 
        $prevNum = $num;
    }
    if($low != -1 ) {
        $compact[] = ($num - $low >= 1) ? sprintf("%d-%d", $low, $num) : $num; 
    }
    return implode(",", $compact);
}

public static function expand($compact) {
    $expanded = Array();
    $compact = explode(",", $compact);
    foreach($compact as $num) {
        if( is_numeric($num) ) {
            $expanded[] = $num;
        } else {
            list($low, $high) = explode("-", $num);
            if( is_numeric($low) && is_numeric($high) && $low < $high) {
                for($i = $low;$i <= $high;$i++) {
                    $expanded[] = $i;
                }
            }
        }
    }
    return $expanded;
}
于 2012-08-09T00:32:44.833 に答える
-2
//Placeholder array
$b = array();

// Slice array (where to slice)
$s = array(11, 21);

foreach ($array as $year => $a) {
    for($i = 0; $i < count($a); $i++) {
        for($ii = 0; $ii < count($s); $ii++) {
            if($i == 0) {
                $b[$year]['<' . $s[$ii]][] = $a[$i];
                break;
            } else if ( isset($a[$i+1]) && $a[$i] < $s[$ii] && $a[$i+1] >=$s[$ii]){
                $b[$year]['<' . $s[$ii]][] = $a[$i];
                if (isset($s[$ii+1])) {
                    $b[$year]['<' . $s[$ii+1]][] = $a[$i+1];
                } else {
                    $b[$year]['>' . $s[$ii]][] = $a[$i+1];
                }
                break;
            } else if ( !isset($s[$ii+1]) && $i == count($a) - 1) {
                $b[$year]['>' . $s[$ii]][] = $a[$i];
                break;
            }
        }
    }
}

$ array

The list of numbers

出力($ b):

array
  2000 => 
    array
      '<11' => 
        array
          0 => int 3
          1 => int 7
      '<21' => 
        array
          0 => int 11
          1 => int 17
      '>21' => 
        array
          0 => int 25
          1 => int 45
  2001 => 
    array
      '<11' => 
        array
          0 => int 2
          1 => int 10
      '<21' => 
        array
          0 => int 11
          1 => int 13
      '>21' => 
        array
          0 => int 34
          1 => int 52
  2002 => 
    array
      '<11' => 
        array
          0 => int 1
          1 => int 10
      '<21' => 
        array
          0 => int 11
          1 => int 20
      '>21' => 
        array
          0 => int 21
          1 => int 52
  2003 => 
    array
      '<11' => 
        array
          0 => int 1
          1 => int 10
      '<21' => 
        array
          0 => int 11
          1 => int 20
      '>21' => 
        array
          0 => int 21
          1 => int 51
  2004 => 
    array
      '<11' => 
        array
          0 => int 1
          1 => int 10
      '<21' => 
        array
          0 => int 11
          1 => int 19
      '>21' => 
        array
          0 => int 21
          1 => int 52
  2005 => 
    array
      '<11' => 
        array
          0 => int 1
          1 => int 10
      '<21' => 
        array
          0 => int 11
          1 => int 20
      '>21' => 
        array
          0 => int 21
          1 => int 52
  2006 => 
    array
      '<11' => 
        array
          0 => int 1
          1 => int 10
      '<21' => 
        array
          0 => int 11
          1 => int 20
      '>21' => 
        array
          0 => int 21
          1 => int 52
  2007 => 
    array
      '<11' => 
        array
          0 => int 1
          1 => int 10
      '<21' => 
        array
          0 => int 11
          1 => int 20
      '>21' => 
        array
          0 => int 21
          1 => int 52

注:ニーズに合わせて値(11)と(21)を変更するだけです。さらに値を追加できます。

于 2012-08-09T00:17:54.313 に答える
-4

ここに例があります:

$query = "SELECT '1-11' Range, COUNT(rank) rank 
    FROM promoted WHERE rank between 1 and 11 
    union all 
    SELECT '12-21' Range, COUNT(rank) rank 
    from promoted 
    where rank between 12 and 21 
    union all 
    SELECT '22-31' Range, count(rank) rank 
    from promoted 
    where rank between 22 and 31 
    union all 
    SELECT '32-40' Range, count(rank) rank 
    from promoted 
    where rank between 22 and 31 
    union all 
    SELECT rank, count(rank) FROM promoted WHERE rank = '40'";  
于 2012-08-09T00:10:20.977 に答える