计算两个数字之间的差异

时间:2017-07-13 11:31:04

标签: php

我想计算两个给定数字之间的差异,例如:
1 - 5或24 - 35

我必须将数组中的数字分开并写下它们之间的差异,如:
1 2 3 4 5或24 25 26 27 28 29 30 31 32 33 34 35

是否有任何PHP函数可以处理该解决方案或任何其他PHP解决方案?

我的代码:

$string = array('1 - 5','9','11','24 - 35'); 

$arr = explode(",", $string);                        
  foreach ($arr as $val) {
    if (preg_match("/ - /", $val)) {

        $val = trim($val);
        $min= trim(substr($val, 0, 2));
        $max = trim(substr($val, -2));

      }
    }       

非常感谢!

6 个答案:

答案 0 :(得分:1)

您应该使用range()

<?php

foreach (range(24, 35) as $number) {
    echo $number."\n";
}


?>

输出:

24
25
26
27
28
29
30
31
32
33
34
35

http://codepad.org/kcozaHbp

答案 1 :(得分:0)

您的$string不是字符串,它是一个数组,因此无需爆炸数组并尝试使用preg_split()来分割字符串。使用range()implode()而不使用循环,

<?php
$arr = array('1 - 5','9','11','24 - 35'); 
foreach ($arr as $val) {
    $range = preg_split("/ - /", $val);
    if(count($range)==2 && $range[0]<$range[1]){
       echo implode(',',range($range[0],$range[1]));
       echo "\n";
    }
}  
?>

Demo with Range without using Loop

答案 2 :(得分:0)

使用range()获取范围内的数字数组。

Source

<?php
$numbers = ['1 - 5','9','11','24 - 35'];
$newNumbers = [];

foreach ($numbers as $i => $num) {
    $numRange = explode('-', str_replace(' ', '', $num));

    if (count($numRange) != 1) {
        $newNumbers = array_merge($newNumbers, range($numRange[0], $numRange[1]));
    } else {
        $newNumbers[] = $num;
    }
}

var_dump($newNumbers);

/**
Outputs

array(19) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(9)
  [6]=>
  int(11)
  [7]=>
  int(24)
  [8]=>
  int(25)
  [9]=>
  int(26)
  [10]=>
  int(27)
  [11]=>
  int(28)
  [12]=>
  int(29)
  [13]=>
  int(30)
  [14]=>
  int(31)
  [15]=>
  int(32)
  [16]=>
  int(33)
  [17]=>
  int(34)
  [18]=>
  int(35)
}

答案 3 :(得分:0)

$string = '1 - 5,9,11,24 - 35';

$arr = explode(',', $string);
foreach ($arr as $val) {
    if (count($values = explode('-', $val)) === 2) {
        $range = range(min(trim($values[0]), trim($values[1])), max(trim($values[0]), trim($values[1])));
        var_dump($range);
    }
}

答案 4 :(得分:0)

希望有所帮助

$string = array('1 - 5','9','11','24 - 35'); 

foreach ($string as $key => $val) {

    preg_match_all("/[0-9]+/", $val, $matches);

    if(count($matches[0]) == 1) {
        //single number
        $string[$key] = $matches[0][0];
    } elseif(count($matches[0]) == 2) {

        $n1 = intval($matches[0][0]);
        $n2 = intval($matches[0][1]);

        $vals = range($n1, $n2);
        $string[$key] = implode(" ", $vals);
    } else {
        throw new \RuntimeException("more than 2 numbers");
    }
}  

print_r($string);

结果:

Array
(
    [0] => 1 2 3 4 5
    [1] => 9
    [2] => 11
    [3] => 24 25 26 27 28 29 30 31 32 33 34 35
)

答案 5 :(得分:0)

您不必要地在阵列上使用了内爆。你需要将数组作为一个数组来处理。

使用range(),您可以很好地生成x - y之间的数字。

也没有必要让正则表达式引擎参与像is there a hyphen in this string这样简单的事情

$string = array('1 - 5','9','11','24 - 35'); 

$new_arr = array();
foreach ($string as $val) {
    if (strpos($val,'-') !== false) {
        $v2 = explode('-', $val);
        foreach (range($v2[0],$v2[1]) as $v)
        $new_arr[] = $v;

    } else {
        $new_arr[] = $val;
    }
}       

print_r($new_arr);

结果:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 9
    [6] => 11
    [7] => 24
    [8] => 25
    [9] => 26
    [10] => 27
    [11] => 28
    [12] => 29
    [13] => 30
    [14] => 31
    [15] => 32
    [16] => 33
    [17] => 34
    [18] => 35
)