如何用PHP比较session中的数据?
$_SESSION 具有以下数据数组。
Array (
[totalprice] => 954
[cart] => Array (
[115] => Array (
[name] => MÅNESKINN
[price] => 268.00
[count] => 1 )
[80] => Array (
[name] => DELFINLEK
[price] => 268.00
[count] => 1 )
[68] => Array (
[name] => OPPDAGELSEN
[price] => 418.00
[count] => 1 ) )
[shipping] => 65 )
现在我需要比较价格并找到最高价格,以便使用以下代码确定运费。
...
$shippingprice = 25.0;
if ( $priceincart > 268 ){
$shippingprice = 65.0;
}
...
$_SESSION['shipping'] = $shippingprice;
如何从数组中找到最高价格?
提前致谢。
The $_SESSION has the following data array.
Array (
[totalprice] => 954
[cart] => Array (
[115] => Array (
[name] => MÅNESKINN
[price] => 268.00
[count] => 1 )
[80] => Array (
[name] => DELFINLEK
[price] => 268.00
[count] => 1 )
[68] => Array (
[name] => OPPDAGELSEN
[price] => 418.00
[count] => 1 ) )
[shipping] => 65 )
Now I need to compare the price and find the highest price in order to determine the shipping charge with the following code.
...
$shippingprice = 25.0;
if ( $priceincart > 268 ){
$shippingprice = 65.0;
}
...
$_SESSION['shipping'] = $shippingprice;
How can I find the highest price from the array?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这个简单的算法:
它迭代购物车商品并测试商品的价格是否大于当前最大值,如果大于则更新最大值。
Try this simple algorithm:
It iterates the cart items and tests if the item’s price is larger than the current maximum and updates the maximum if it’s larger.
这应该可以工作,尽管它假设 PHP 版本 >= 5.3。
给定一个起始最小价格(0 零),array_reduce 将在 $array['cart'] 的每个元素处调用回调函数(其中每个元素也是一个数组),然后调用的函数将返回 $acc 的最大值或 $in['price']。该最大值将在下次调用时传递给回调函数(如 $acc)。
如果 array_reduce() 返回 NULL,$max_price 将设置为零。
This should work, although it assumes a PHP version >= 5.3.
Given a starting smallest price (0 zero), array_reduce will call the callback function at each element of $array['cart'] (where each element is also an array), and then the called in function will return the maximum of $acc or $in['price']. This maximum value will then be passed in to the callback function (as $acc) the next time it is called.
In the event that array_reduce() returns NULL, $max_price is set to zero.