如何将关联数组附加到另​​一个数组的末尾?

时间:2016-08-26 22:12:01

标签: php arrays

我正在为我的购物车工作并遇到问题。我真的试图自己解决,但同样的问题。 基本上人们会从下拉菜单中选择鞋子大小来发送到购物车。并且代码将检查:

  • - 如果会话购物车中已有值:如果id和size的值相同,那么我们会增加现有值的数量
  • - 如果会话购物车中已有值:如果id值相同但大小值不同,那么它是一个新值,追加到 现有价值。

    对于第一次检查代码工作正常,它会检查会话购物车中的现有值,如果它是相同的ID和相同的大小,则数量会增加。

    问题出现在第二次检查中:当我在下拉列表中选择另一个尺码但具有相同的商品ID时,新值(id和尺寸)只是删除购物车中的现有值。

我想要的是:如果item id相同但大小不同,那么它是一个新值,并且该数组值需要作为新值附加到会话购物车的末尾

这是我的代码:

<?php
  $items = array(
  array('id' => '1', 'desc' => 'sneaker','price' => 24.95, 'size'=>'424546'),
  array('id' => '2', 'desc' => 'Reebok','price' => 200, 'size'=>'323845'),
  array('id' => '3', 'desc' => 'Songs of the Goldfish (2CD set)','price' => 19.99),
  array('id' => '4', 'desc' => 'Simply JavaScript (SitePoint)', 'price' => 39.95)
  );

session_start();
 if (!isset($_SESSION['cart'])) {
 $_SESSION['cart'] = array();
 }

if (isset($_POST['action']) and $_POST['action'] == 'Buy')  {
 $size="";
 //Get the values for the post and make sure they are integers
 $pid = filter_var( $_POST['id'], FILTER_VALIDATE_INT, array('min_range'=> 1) );
 $size = $_POST['doSize'];
 if (isset($_SESSION['cart'][$pid])) {

    foreach ($_SESSION['cart'] as $key) {

if ($key['id'] === $pid and $key['size'] !== $size) {
        echo "NOT SAME SIZE";
        $si = $size;
$_SESSION['cart'][$key['id']] = array('id'=>$pid, 'size'=>$si, 'quantity'=>1);  

      }

      elseif ( $key['id'] === $pid and $key['size']=== $size ) {
         $_SESSION['cart'][$key['id']]['quantity']++;
            echo "YES CORRECT";
      } 
   }//end foreach
   print_r($_SESSION['cart']);
 }//end isset session cart

 else{
$_SESSION['cart'][$pid]= array('id'=>$pid, 'size'=>$size, 'quantity'=>1);

 foreach ($_SESSION['cart'] as $key) {
      echo $key['id'];
    }

print_r($_SESSION['cart']);
   }
 }//END POST 
include'cat.html';

//unset($_SESSION['cart']);
 ?>

这里是cart.html

<!DOCTYPE html>
  <head>
  <title>Product catalog</title>
  <meta http-equiv="content-type"
  content="text/html; charset=utf-8" />
  <style type="text/css">
 table {
 border-collapse: collapse;
 }
 td, th {
 border: 1px solid black;
 }
 </style>
 </head>
<body>
 <p>Your cart contains <?php echo count($_SESSION['cart']);?> item(s).</p>
 <p><a href="?cart">View your cart</a></p>
 <table border="1">
 <thead>
 <tr>
 <th>Item Description</th>
 <th>Price</th>
 <th>Size</th>
 </tr>
 </thead>

<tbody>
 <?php foreach ($items as $item): ?>
 <tr>
 <td><?php echo $item['desc']; ?></td>
 <td>
 $<?php echo number_format($item['price'], 2); ?>
 </td>
 <td>

<form action="" method="post">

 <?php if (isset($item['size'])) { ?>

 <select name="doSize">
 <?php foreach(str_split($item['size'], 2) as $size):?>
 <option value="<?php echo $size;?>"><?php echo $size;?></option>
 <?php endforeach;?>


 </select>
 <?php }?>
  </td>

 <td>
 <div>
 <input type="hidden" name="id" value="<?php echo $item['id']; ?>"/>
 <input type="submit" name="action" value="Buy"/>
 </div>
 </form>
 </td>
 </tr>
 <?php endforeach; ?>
 </tbody>
 </table>
 <p>All prices are in imaginary dollars.</p>
 </body> 
 </html>

2 个答案:

答案 0 :(得分:1)

您的foreach循环错误。当您看到与用户选择的ID或大小不同的购物车条目时,它并不意味着找不到它,因为它可能位于阵列的不同元素中。您需要等到循环结束才能知道是否找到了该项目。

$found = false;
foreach ($_SESSION['cart'] as &$key) {
    // Use &$key reference so we can modify the element
    if ( $key['id'] === $pid and $key['size']=== $size ) {
        $key['quantity']++;
        echo "YES CORRECT";
        $found = true;
        break;
    }
}//end foreach
if (!$found) {
    $_SESSION['cart'][] = array('id' => $pid, 'size' => $size, 'quantity' => 1);
}

此外,您无法使用$pid作为$_SESSION['cart']的关键字,因为您可以使用相同的$pid使用不同的尺寸,并且关联数组可以使用.search。 t有多个具有相同键的元素。

答案 1 :(得分:-2)

$ example_parent_array = array('example_1','example_2');

$ example_parent_array ['example_1'] = array('child_array_index'=&gt;“示例”);

echo $ example_parent_array ['example_1'] ['child_array_index']; //输出:“示例”