【问题标题】:how to find the largest and smallest value in a string如何找到字符串中的最大值和最小值
【发布时间】:2016-02-15 14:04:36
【问题描述】:

您好,我有一个字符串,我想从该字符串中找到最小值和最大值,并将其存储在一个变量中。

Array
(
    [0] => 21,50
)

【问题讨论】:

  • 最大的...如字符数或数值? (21,50) 是字符串还是您想用逗号分割并从这些值中获取?
  • 你有一个实际的数组还是一个字符串?无论哪种方式,您都可能希望将值放入一个数组中,然后使用 max()

标签: php arrays string sorting


【解决方案1】:

您应该使用maxmin 函数。

$arr = ['21,50'];
echo max(explode(',',$arr[0])); // 50
echo min(explode(',',$arr[0])); // 21

希望对你有帮助:)

【讨论】:

    【解决方案2】:

    如果你有一个字符串$a,那么你可以分解它,得到一个数组,然后你可以使用内置函数min()max()

    $a = "21,50";
    $arr = explode($a);
    $maxValue = max($arr);
    $minValue = min($arr);
    

    您也可以通过分解数组键来实现此目的。

    $myArr = array("21,50");
    $tmpArr = explode($myArr[0]);
    $maxVal = max($tmpArr);
    $minVal = min($tmpArr);
    

    【讨论】:

      猜你喜欢
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 2017-07-29
      相关资源
      最近更新 更多