没有获得Money String Array的最大值和最小值

时间:2022-09-08 22:50:28

This is my array

这是我的阵列

Array ( [0] => $30.00 [1] => $200.00 [2] => $138.00 [3] => $100.00 [4] => $30.00 
[5]=>$30.00 [6] => $30.00 )

Trying to get the max and min value from this array

试图从这个数组中获取最大值和最小值

echo $maxprice = max($list);
echo $minprice = min($list);

I get $30.00 for max and $100.00 for min

我得到最高30.00美元和最低100.00美元

I am guessing this is due to the values are in money string.

我猜这是由于价值在金钱串。

Can someone please tell me how I could get the real max and min for this array? Is this really due to money string?

有人可以告诉我如何获得这个数组真正的最大值和最小值?这真的是因为金钱串?

Thanks in advance

提前致谢

Note This array is stripped down from a multidimensional array. If it was normal array, it would be straightforward

注意此数组从多维数组中删除。如果它是正常的数组,那将是直截了当的

3 个解决方案

#1


1  

You need to use decimal values (without the "$") in your array. The dollar sign should only be applied when outputting to display.

您需要在数组中使用十进制值(不带“$”)。只有在输出到显示时才应用美元符号。

To get the string converted to floats, you can just array_walk() the array.

要将字符串转换为浮点数,您可以只使用array_walk()数组。

If on PHP 5.3 or greater, I like to use closures like this

如果在PHP 5.3或更高版本上,我喜欢使用这样的闭包

array_walk($array, function (&$value, $key) {
   $value = (float)ltrim($value, '$');
});

If in older version of PHP, you would need to define a separate function and call the function in array_walk like this

如果在旧版本的PHP中,您需要定义一个单独的函数并像这样调用array_walk中的函数

array_walk($array, 'strip_dollar_signs');

function strip_dollar_signs (&$value, $key) {
    $value = (float)ltrim($value, '$');
}

Then just sort your array as you typically would

然后就像通常那样对数组进行排序

sort($array, SORT_NUMERIC);

#2


2  

Remove the '$' sign to make sure php treats the values as numbers

删除'$'符号以确保php将值视为数字

#3


1  

consider storing numeric values only, unless you work with more than one currency. this code should get you minimum and maximum

考虑仅存储数值,除非您使用多种货币。此代码应该为您提供最小和最大

$copy = $list;
foreach($copy as $key=>$value)$copy[$key] = str_replace('$', '', $value);

echo $maxprice = max($copy);
echo $minprice = min($copy);

#1


1  

You need to use decimal values (without the "$") in your array. The dollar sign should only be applied when outputting to display.

您需要在数组中使用十进制值(不带“$”)。只有在输出到显示时才应用美元符号。

To get the string converted to floats, you can just array_walk() the array.

要将字符串转换为浮点数,您可以只使用array_walk()数组。

If on PHP 5.3 or greater, I like to use closures like this

如果在PHP 5.3或更高版本上,我喜欢使用这样的闭包

array_walk($array, function (&$value, $key) {
   $value = (float)ltrim($value, '$');
});

If in older version of PHP, you would need to define a separate function and call the function in array_walk like this

如果在旧版本的PHP中,您需要定义一个单独的函数并像这样调用array_walk中的函数

array_walk($array, 'strip_dollar_signs');

function strip_dollar_signs (&$value, $key) {
    $value = (float)ltrim($value, '$');
}

Then just sort your array as you typically would

然后就像通常那样对数组进行排序

sort($array, SORT_NUMERIC);

#2


2  

Remove the '$' sign to make sure php treats the values as numbers

删除'$'符号以确保php将值视为数字

#3


1  

consider storing numeric values only, unless you work with more than one currency. this code should get you minimum and maximum

考虑仅存储数值,除非您使用多种货币。此代码应该为您提供最小和最大

$copy = $list;
foreach($copy as $key=>$value)$copy[$key] = str_replace('$', '', $value);

echo $maxprice = max($copy);
echo $minprice = min($copy);