2017-04-04 48 views
0

继内更新数据是我的PHP代码:PHP:如何在一个数组是一个数组

while($row = $resp->fetch_assoc()) 
{ 
    $itemArray = array(
     array(
     'name' => $row["product_name"], 
     'id' => $row["id"], 
     'image' => $row['images'], 
     'discount' => $row["discount"], 
     'quantity' => $min_quantity, 
     'price' => $row["price"] 
    ) 
    ); 
} 

if(!empty($_SESSION["cart_item"])) 
{ 
    if(in_array($itemArray, $_SESSION["cart_item"])) 
    { 
     foreach($_SESSION["cart_item"] as $item) 
     { 
      if(in_array($item, $itemArray)) 
       $_SESSION["cart_item"][$item]["quantity"] = $min_quantity; 
      break; 
     } 
    } 
    else 
    { 
     $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray); 
    } 
} 
else 
{ 
    $_SESSION["cart_item"] = $itemArray; 
} 

执行此代码我得到这样的回应后:

Array 
(
[0] => Array 
    (
     [name] => Girls Designer Dress 
     [id] => 4 
     [image] => s:146:"2017/march/meetfashion01-04-2017_12-19-07_am.jpg,2017/march/meetfashion01-04-2017_12-19-08_am 
.jpg,2017/march/meetfashion01-04-2017_12-19-09_am.jpg"; 
     [discount] => 10 
     [quantity] => 1 
     [price] => 1200 
    ) 

) 

什么我试图达到的目的是,如果用户在购物车中添加了相同的产品,而这些产品的数据我们已经得到响应,而不是再次创建阵列,并将其从12的同一产品的一个接一个的i want to just update the quantity合并。

我坚持在这部分代码

foreach($_SESSION["cart_item"] as $item) 
{ 
    if(in_array($item, $itemArray)) 
    $_SESSION["cart_item"][$item]["quantity"] = $min_quantity; 
    break; 
} 

我已经尝试过很多次,如果遇到同样的产品,然后通过1相同的产品增加的数量和不创建一个以上的阵列。

任何人都可以帮助这个逻辑和代码?

+0

$ _SESSION [“cart-item”]的价值是什么?如果它是一个包含购物车信息的数组,那么循环它并不是一条可行的路。 – FrenchMajesty

+0

是没有值,则这将在它'阵列 ( [0] =>数组 ( [名称] =>女孩设计连衣裙 [ID]加入=> 4 [图像] => S: 146:“2017/march/meetfashion01-04-2017_12-19-07_am.jpg,2017/march/meetfashion01-04-2017_12-19-08_am .jpg,2017/march/meetfashion01-04-2017_12-19-09_am。 JPG“; [折扣] => 10 [量] => 1 [价格] => 1200 ) )'如果存在,则阵列将被更新,并且该阵列将来自'0'延伸到从'[0] => Array'到'[1] => Array'这样的'1'我想更新[0]中出现的值 –

回答

1

$ _SESSION [ “车项目”]单个阵列样的?

Array 
(
    [name] => Girls Designer Dress 
    [id] => 4 
    [image] => s:146:"2017/march/meetfashion01-04-2017_12-19-07_am.jpg,2017/march/meetfashion01-04-2017_12-19-08_am 
.jpg,2017/march/meetfashion01-04-2017_12-19-09_am.jpg"; 
    [discount] => 10 
    [quantity] => 1 
    [price] => 1200 
) 

如果是的话那就不叫一个foreach,而不只是直接检查如果车项目是在$ itemArray使用array_search()

(函数在数组中搜索并且如果发现了针,则返回其索引,否则返回false)。

$cartItemPosition = array_search($_SESSION['cart-item'], $itemArray); 

if($cartItemPosition == false) { 
    // Not found in array, so create new entry 
    $itemArray.push($_SESSION['cart-item']); 

}else { 
    // Found in array so edit existing entry 

    $itemArray[$cartItemPosition]['quantity'] = 999; 
} 
+0

以及如果添加新产品会怎样?它会创建一个新的数组并将其追加到前一次? –

+0

@AkshayShrivastav。你会在'//未找到'循环中这样做。另外为什么重新创建一个新的数组?只需将其附加到已有的。 – FrenchMajesty

+0

这一个完美的工作:)我创建了一个附加数组 –

0

下面的作品,如果$itemArray只有1项:

更改为:

if(!empty($_SESSION["cart_item"])) 
    { 
     if(in_array($itemArray[0], $_SESSION["cart_item"])) 
      { 
       foreach($_SESSION["cart_item"] as $index => $item) 
        { 
         if(in_array($item, $itemArray)) 
          $_SESSION["cart_item"][$index]["quantity"] = $_SESSION["cart_item"][$index]["quantity"] + 1; 
         break; 
        } 
      } 
     else 
      { 
       $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray); 
      } 
    } 
else 
    { 
     $_SESSION["cart_item"] = $itemArray; 
    } 
  • 添加[0]第一条语句的in_array如果。
  • 在foreach循环中添加$index并使用它。

您的代码之前从未去过if语句。如果$itemArray始终在数组内部包含一个数组(这就是我们使用[0]的原因),那么它应该是这样的。

如果有可能$itemArray有多个项目,那么我们将不得不根据需要编辑答案。

也不确定,但如果您fetching loop是可能返回多个项目,那么这是错误的,因为你只会有最后的结果。如果你知道你将只有1个结果,那么它是一切都很好。

while($row = $resp->fetch_assoc()) 
    { 
     $itemArray = array(
          array(
            'name' => $row["product_name"], 
            'id' => $row["id"], 
            'image' => $row['images'], 
            'discount' => $row["discount"], 
            'quantity' => $min_quantity, 
            'price' => $row["price"] 
           ) 
         ); 
    }