php - 从逗号分隔的值范围中获取最小值和最大值

php  - 从逗号分隔的值范围中获取最小值和最大值

问题描述:

I have simple values ranges as

0-50000,100000-250000,250000-500000,1000000,

This may be only 0-50000 or 0-50000,100000-250000, or 0-50000,250000-500000, or 1000000, if 1000000, get max value infinite

In any one of the above mentioned cases selected, i want to get minimum and maximum values.

How to do this?

我的简单值范围为 p>

  0-50000,  100000-250000,250000-500000,1000000,
  code>  pre> 
 
 

这可能只有0-50000或0-50000,100000-250000,或0-50000,250000- 500000,或1000000, if 1000000,获取最大值无限 p>

在上述任何一种情况下,我想获得最小值和最大值。 p> \ n

怎么做? p> div>

This one simple solution using Explode()

$val = '0-50000,100000-250000,250000-500000,1000000';
$min = '';
$max = '';

$valExlode=explode(",",$val);

foreach ( $valExlode as $singleVal ) {
   $singleValExplode = explode ("-",$singleVal);
   foreach ( $singleValExplode as $singleUnit ) {
      if ( $min == '' ) {
        $min = (int)$singleUnit;
      }
      if ( $max == '' ) {
        $max = (int)$singleUnit;
      }
      if ( $singleUnit <= $min ) {
        $min = (int)$singleUnit;
      }
      if ( $singleUnit >= $max ) {
        $max = (int)$singleUnit;
      }
  }
}
echo "Minimum: ".$min;
echo "Maximum: ".$max;

as #Barmar suggested