PHP的foreach进入无限循环,数组存储在会话中 [英] php foreach goes into infinite loop, array stored in session

查看:187
本文介绍了PHP的foreach进入无限循环,数组存储在会话中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有的作品都是完美的,但是当购物车里有更多的物品时,任何物品的数量(列表中的最后一个物品除外)都会改变,下面的代码进入无限循环,我已经通过放置print_r语句。



进入无限循环的代码部分:

  if(isset($ _ POST ['item_to_adjust'])&& $ _POST ['item_to_adjust']!=){
//执行一些代码
$ item_to_adjust = $ _POST [ 'item_to_adjust'];
$ quantity = $ _POST ['quantity'];
$ quantity = preg_replace('#[^ 0-9] #i','',$ quantity); ($ quantity> = 100){$ quantity = 99; }
if($ quantity <1){$ quantity = 1; }
if($ quantity ==){$ quantity = 1; }
$ i = 0;
foreach($ _SESSION [cart_array]为$ each_item){
print_r($ each_item);
$ i ++; ($ key $item_id&& $ value == $ item_to_adjust)
while(list($ key,$ value)= each($ each_item)){
{
//这个项目已经在购物车里了,所以让我们使用array_splice()来调整它的数量
array_splice($ _ SESSION [cart_array],$ i-1,1,array(array(item_id=&$ item_to_adjust,quantity=> $ quantity)));
} //关闭条件
} //关闭while循环
} //关闭foreach循环

$ b $ p

这是数组在第一个项目被添加时被初始化的方式。 b

  $ _ SESSION [cart_array] = array(0 => array(item_id=> $ pid,quantity=> 1) ); 

如果需要其他细节,请告诉我们。



更新:假设购物车中有三件物品。而我更改第三项的数量。这行得通。
但是,如果我改变第二项的数量,脚本达到最大执行时间,第二和第三项在购物车中无限重复。

解决方案

  foreach($ _SESSION [cart_array] as $ item_key => $ each_item){
if($ item_to_adjust == $ each_item [item_id]){
$ _SESSION [cart_array] [$ item_key] [quantity] = $ quantity;






$ b

这仍然修改循环内的数组(不是酷),但它不会混淆索引。


All works perfectly, but when there are more one items in the cart.. and the the quantity of any item (except the last item in list) is changed the code below goes into infinite loop, i have verified it by placing print_r statements in it.

The part of the code that goes into infinite loop:

if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
    // execute some code
    $item_to_adjust = $_POST['item_to_adjust'];
    $quantity = $_POST['quantity'];
    $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
    if ($quantity >= 100) { $quantity = 99; }
    if ($quantity < 1) { $quantity = 1; }
    if ($quantity == "") { $quantity = 1; }
    $i = 0;
    foreach ($_SESSION["cart_array"] as $each_item) { 
              print_r($each_item);
              $i++;
              while (list($key, $value) = each($each_item)) {
                  if ($key == "item_id" && $value == $item_to_adjust) {
                      // That item is in cart already so let's adjust its quantity using array_splice()
                      array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
                  } // close if condition
              } // close while loop
    } // close foreach loop
}

P.S.

This is how the array is initialized when 1st item is added.

$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));

If any other detail is required please let me know..

Update : Suppose there are three items in in a cart. And i change the quantity of the 3rd item. It work. But if i change the quantity of the 2nd item, The script hits max execution time and the 2nd and 3rd items are repeated in the cart infinitely.

解决方案

foreach ($_SESSION["cart_array"] as $item_key => $each_item) {
    if ($item_to_adjust == $each_item["item_id"]) {
        $_SESSION["cart_array"][$item_key]["quantity"] = $quantity;
    }
}

This is still modifying the array inside the loop (not cool), but it does not mess with indexes.

这篇关于PHP的foreach进入无限循环,数组存储在会话中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆