我需要在php中为产品购物车创建一个多维数组。我需要购物车看起来像这样。
由一系列产品组成的订单数组。
主阵列应该像订单1订单2订单3等。 在每个主要数组产品中都有名称,产品代码。
Array ( [0] => Array
( [0] => Array
( [name] => Heart Choclates [code] => LFB-P-10 [qty] => 1 [type] => main [price] => 1200 [stock] => 5 [image] => choclates-valentines-day.jpg [quantity] => 12 [expdate] => May 25th 2017 [exptime] => 08:00 AM to 09:00 AM [expdtype] => Fixed time delivery
)
)
)
//MySqli query - get details of item from db using product code
$results = $mysqli->query("SELECT * FROM products WHERE product_code='$product_code' LIMIT 1");
$obj = $results->fetch_object();
if ($results) { //we have the product info
//prepare array for the session variable
if ($product_price == $obj->price) {
$productextra= array(
$productslist = array(
'name' => $obj->product_name,
'code' => $product_code,
'qty' => $product_qty,
'type' => 'main',
'price' => $obj->price,
'stock' => $obj->stock,
'image' => $obj->image,
'quantity' => $obj->quantity,
'expdate' =>$_SESSION['dateofdelivery'],
'exptime' =>$_SESSION['timeslot'],
'expdtype'=>$_SESSION['typeofdelivery']
)
);
}
如何修改此代码才能获得它。
Array ( [0] => Array ( [name] => Birthday Pink [code] => KB-P-5 [qty] => 1 [type] => main [price] => 600 [stock] => 7 [image] => pink-roses.jpg [quantity] => 10 [expdate] => May 25th 2017 [exptime] => 12:00 PM to 04:00 PM [expdtype] => Standard delivery ) [1] => Array ( [name] => Signature Cake [code] => KB-P-7 [qty] => 1 [type] => addon [price] => 0 [stock] => 9 [image] => signature-cake.jpg [expdate] => May 25th 2017 [exptime] => 12:00 PM to 04:00 PM [expdtype] => Standard delivery ) [2] => Array ( [name] => Truffle Cake [code] => KB-P-8 [qty] => 1 [type] => addon [price] => 10 [stock] => 7 [image] => truffle-cake.jpg [expdate] => May 25th 2017 [exptime] => 12:00 PM to 04:00 PM [expdtype] => Standard delivery ) )
答案 0 :(得分:1)
试试这个
$productextra = array();
if ($product_price == $obj->price)
{
$productextra[] = array(
$productslist => array(
'name' => $obj->product_name,
'code' => $product_code,
'qty' => $product_qty,
'type' => 'main',
'price' => $obj->price,
'stock' => $obj->stock,
'image' => $obj->image,
'quantity' => $obj->quantity,
'expdate' =>$_SESSION['dateofdelivery'],
'exptime' =>$_SESSION['timeslot'],
'expdtype'=>$_SESSION['typeofdelivery']
)
);
}
上面的代码将输出如
Array ( [0] => Array ( [productslist_value] => Array ( [name] => name ) ) )
或
$productextra = array();
if ($product_price == $obj->price)
{
$productextra[] = array(
array(
'name' => $obj->product_name,
'code' => $product_code,
'qty' => $product_qty,
'type' => 'main',
'price' => $obj->price,
'stock' => $obj->stock,
'image' => $obj->image,
'quantity' => $obj->quantity,
'expdate' =>$_SESSION['dateofdelivery'],
'exptime' =>$_SESSION['timeslot'],
'expdtype'=>$_SESSION['typeofdelivery']
)
);
}
输出如
Array ( [0] => Array ( [0] => Array ( [name] => name ) ) )....
答案 1 :(得分:0)
您可以运行以下代码Here并使用您想要的代码
<?php
$productextra= array(
$productslist = array(
'name' => "Heart Choclates",
'code' => "LFB-P-10",
'qty' => 1,
'type' => 'main',
'price' => 1200,
'stock' => "5",
'image' => "choclates-valentines-day.jpg",
'quantity' => "12",
'expdate' => "May 25th 2017",
'exptime' => "08:00 AM to 09:00 AM",
'expdtype'=> "Fixed time delivery"
)
);
$productslist = array(
'name' => "Heart Choclates",
'code' => "LFB-P-10",
'qty' => 1,
'type' => 'main',
'price' => 1200,
'stock' => "5",
'image' => "choclates-valentines-day.jpg",
'quantity' => "12",
'expdate' => "May 25th 2017",
'exptime' => "08:00 AM to 09:00 AM",
'expdtype'=> "Fixed time delivery"
);
$main_array=array(
$productextra= array(
$productslist = array(
'name' => "Heart Choclates",
'code' => "LFB-P-10",
'qty' => 1,
'type' => 'main',
'price' => 1200,
'stock' => "5",
'image' => "choclates-valentines-day.jpg",
'quantity' => "12",
'expdate' => "May 25th 2017",
'exptime' => "08:00 AM to 09:00 AM",
'expdtype'=> "Fixed time delivery"
)
)
);
echo "<pre>";
print_r($productextra);
echo "<br>";
print_r($productslist);
echo "<br>";
print_r($main_array);
?>
输出
Array
(
[0] => Array
(
[name] => Heart Choclates
[code] => LFB-P-10
[qty] => 1
[type] => main
[price] => 1200
[stock] => 5
[image] => choclates-valentines-day.jpg
[quantity] => 12
[expdate] => May 25th 2017
[exptime] => 08:00 AM to 09:00 AM
[expdtype] => Fixed time delivery
)
)
Array
(
[name] => Heart Choclates
[code] => LFB-P-10
[qty] => 1
[type] => main
[price] => 1200
[stock] => 5
[image] => choclates-valentines-day.jpg
[quantity] => 12
[expdate] => May 25th 2017
[exptime] => 08:00 AM to 09:00 AM
[expdtype] => Fixed time delivery
)
Array
(
[0] => Array
(
[0] => Array
(
[name] => Heart Choclates
[code] => LFB-P-10
[qty] => 1
[type] => main
[price] => 1200
[stock] => 5
[image] => choclates-valentines-day.jpg
[quantity] => 12
[expdate] => May 25th 2017
[exptime] => 08:00 AM to 09:00 AM
[expdtype] => Fixed time delivery
)
)
)