我正在做一个简单的购物应用程序,用户可以根据标题,价格,数量等从商店中选择书籍。目前,书籍的所有细节都保存在一个关联数组中,如下所示:
$menu = array(
array ('id' => '1', 'catagory' => 'newbooks','title' => 'Alex','price' => 4.95, 'desc' => 'bbbb'),
array('id' => '2','catagory' => 'newbooks','title' => 'David ','price' => 5.95, 'desc' => 'sss'),);
}
我想要实现的是合并或删除图书ID的重复条目,每个图书标题也将打印在一行中,其中包含'title','id','price'
例如:
Alex qty:2 price 4.95
David qty:2 price 5.95
等等... 这是我的购物车档案:
$buy = array();
$qty = 0;
$total = 0;
foreach ($_SESSION['buy'] as $id) {
foreach ($menu as $book ) {
if ($book['id'] == $id) {
$buy[] = $book;
$total += $book['price'];
}
}
}
}
if (count($buy) > 0)
if (count($book) > 0)
{
echo"<table>";
echo"<tr><th>Item Description</th><th>Quantity</th><th>Price</th></tr>";
foreach ($buy as $book){
$f_price = sprintf("%0.2f", $book["price"]);
echo"<tr><td>" .$book["title"] ."</td>";
echo"<td>" .$qty."</td><";
echo"<td>" .$f_price."</td></tr>";
}
echo"<tr><td>Total</td><td>".$total."</td></tr>";
echo"</table>";
}
我将非常感谢任何帮助或建议因为我花了很长时间来解决这个问题尝试使用独特的功能,对于语句foreach循环但是徒劳我仍然是新手也请尽量有点宽容。干杯!
答案 0 :(得分:0)
如果它只是基于id,那么在构建数组时,将book id存储为较大数组中的键,因此你的结束数组将是:
$books = array(
1=> array ('id' => '1', [...]),
2=> array ('id' => '2', [...])
);
您还可以添加一些代码,在您尝试为该数组分配值时检查该代码是否已设置。
if(!isset($books[$book_id])){
//Add to the array
}else{
//Maybe display an error?
}
PS:类别中只有一个a
。
答案 1 :(得分:0)
这是一个非常麻烦的方式来对您的收藏中的书籍进行库存概览,但除此之外:)
如果我要使用这样的数组,(我永远不会这样做:))我会首先选择所有唯一键,然后逐个浏览它们,计算它出现的次数$menu
数组。
如下:
$unique_books = array_unique($menu);
foreach($unique_books as $book){
$qty = array_keys($menu, $book["id"])
$book["qty"] = $qty;
$stock = $book;
}
正如你所看到的,现在$ stock将是唯一的数组,添加了一个数量,包含次数,在$ menu中看到了id。 这只是一个快速的代码块,应该是testet,但我认为理论是有效的。正如我之前提到的,你首先做出这个的方式,并不是很聪明,你应该重新考虑整个事情:)
答案 2 :(得分:0)
如果您不想重复,请使用book_id作为购买数组的关键
示例:
$menu = array(
array('id' => '1', 'catagory' => 'newbooks','title' => 'Alex','price' => 4.95, 'desc' => 'bbbb'),
array('id' => '2','catagory' => 'newbooks','title' => 'David ','price' => 5.95, 'desc' => 'sss')
);
$session=array(1,2,2,2,1);
$buy = array();
foreach ($session as $id) {
foreach ($menu as $book) {
if ($book['id'] == $id) {
if(isset($buy[$id])){
$buy[$id]['qty']++; // increase quantity
$buy[$id]['total'] += $book['price']; // total price only for this id
$buy['total'] += $book['price']; // All ids price total
}else{
$buy[$id] = $book; // assign new id to the buy array
$buy[$id]['qty'] = 1; // quantity reset to 1
$buy[$id]['total'] = $book['price']; // id price
$buy['total'] += $book['price']; // total price
}
}
}
}
print_r($buy);
$menu = array(
array('id' => '1', 'catagory' => 'newbooks','title' => 'Alex','price' => 4.95, 'desc' => 'bbbb'),
array('id' => '2','catagory' => 'newbooks','title' => 'David ','price' => 5.95, 'desc' => 'sss')
);
$session=array(1,2,2,2,1);
$buy = array();
foreach ($session as $id) {
foreach ($menu as $book) {
if ($book['id'] == $id) {
if(isset($buy[$id])){
$buy[$id]['qty']++; // increase quantity
$buy[$id]['total'] += $book['price']; // total price only for this id
$buy['total'] += $book['price']; // All ids price total
}else{
$buy[$id] = $book; // assign new id to the buy array
$buy[$id]['qty'] = 1; // quantity reset to 1
$buy[$id]['total'] = $book['price']; // id price
$buy['total'] += $book['price']; // total price
}
}
}
}
print_r($buy);
你可以在这里测试一下!
答案 3 :(得分:0)
未经测试但是这应该可以使用array_count_values执行您想要的数量,以获得您要查找的数量以及一些更改,以使脚本更高效,更易于管理。
<?php
//the ids of the items the user wants to buy
$ids=$_SESSION['buy'];
//Your database of items
$menu = array(
array ('id' => '1', 'catagory' => 'newbooks','title' => 'Alex','price' => 4.95, 'desc' => 'bbbb'),
array('id' => '2','catagory' => 'newbooks','title' => 'David ','price' => 5.95, 'desc' => 'sss')
);
//Create a reverse look-up for the database
$lookUp=array();
foreach($menu as $i=>$v)$lookUp[$v['id']]=$menu[$i];
//populate the cart with items, their quantities, and a total
$cart = array();
$total = 0;
foreach (array_count_values($ids) as $id=>$quantity)
{
if (!array_key_exists($id, $lookUp))continue;
$item=$lookUp[$id];
$itemTotal=$item['price']*$quantity;
$total+=$itemTotal;
$cart[]=array_merge($item,array('quantity'=>$quantity,'itemTotal'=>$itemTotal));
}
if(count($cart) > 0)
{
echo '<table>';
echo '<tr><th>Item Description</th><th>Quantity</th><th>Price</th><th>Item Total</th></tr>';
foreach ($cart as $item)
{
echo sprintf('<tr><td>%s</td><td>%s</td><td>%0.2f</td><td>%0.2f</td></tr>',
$item["title"],$item['quantity'],$item['price'],$item['itemTotal']);
}
echo '<tr><td colspan="3" style="text-align:center;">Total</td><td>'.$total.'</td></tr>';
echo '</table>';
}
答案 4 :(得分:-1)
您应该将book id和qty作为数组行添加到会话中,而不是合并重复项。
在会话开始..
if (!isset($_SESSION['buy']))
$_SESSION['buy'] = array();
添加新..
$new_book = $added_book; // just for example
$qty = $added_qty; // Added qty
// Check if book already exists in shopping cart and update qty
if (array_key_exists($new_book['id'], $_SESSION['buy'])) {
$_SESSION['buy'][$new_book['id']] += $qty;
} else {
$_SESSION['buy'][$new_book['id']] = $qty;
}
然后在购物车视图中直接循环您的商品..
foreach($menu as $book ) {
if (array_key_exists($book['id'], $_SESSION['buy'])) {
// User bought this book..
$qty = $_SESSION['buy'][$book['id']];
}
}