如何用PHP比较session中的数据?

发布于 2024-08-17 07:53:45 字数 671 浏览 10 评论 0原文

$_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生不灭 2024-08-24 07:53:45

尝试这个简单的算法:

$max = 0;
foreach ($_SESSION['cart'] as $item) {
    if ($item['price'] > $max) {
        $max = $item['price'];
    }
}

它迭代购物车商品并测试商品的价格是否大于当前最大值,如果大于则更新最大值。

Try this simple algorithm:

$max = 0;
foreach ($_SESSION['cart'] as $item) {
    if ($item['price'] > $max) {
        $max = $item['price'];
    }
}

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.

掩饰不了的爱 2024-08-24 07:53:45

这应该可以工作,尽管它假设 PHP 版本 >= 5.3。

$max_price = array_reduce($array['cart'], function($acc, $in) { 
    return max($acc, $in['price']); 
}, 0) or $max_price = 0;

给定一个起始最小价格(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.

$max_price = array_reduce($array['cart'], function($acc, $in) { 
    return max($acc, $in['price']); 
}, 0) or $max_price = 0;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文