自上午9点以来,我一直在我的桌子上敲打我的头,经过我所做的所有搜索,我仍然没有得到我需要的结果。我所拥有的是使用。
动态创建的数组 array_push($this->product_info,
$subline1,
$subline2,
$subcarving,
$subpicture,
$extra_cost,
$subtotal);
if(!empty($this->cart)){
array_push($this->cart, $this->product_info);
}
else
{
$this->cart = $this->product_info;
}
设置完成后,用户可以添加其他产品或继续结帐。如果他们添加了另一个产品,我会使用array_push()附加新数组,如您所见。所以现在数组有更多的1个数组。我希望能够从每个数组中只抓取两个元素。所以我写了这个:
$count = count($_SESSION['cart']);
for($i=0;$i<$count;$i++){
foreach($_SESSION['cart'][$i] as $key => $value){
echo "This is element $i with key $key: $value<br />";
}
}
这是输出到浏览器的内容:
This is element 0 with key 0: VP1021-G
This is element 0 with key product_id: VP1021-G
This is element 0 with key 1: Pendant
This is element 0 with key product_type: Pendant
This is element 0 with key 2: Vertical
This is element 0 with key product_format: Vertical
This is element 0 with key 3: Goldtone
This is element 0 with key setting_color: Goldtone
This is element 0 with key 4: 125
This is element 0 with key price: 125
This is element 0 with key 5: N/A
This is element 0 with key 6: N/A
This is element 0 with key 7: Black and White
This is element 0 with key 8: 01_faces.jpg
This is element 0 with key 9: 0
This is element 0 with key 10: 125
This is element 1 with key 0: Array
如果我使用它,我可以抓住我想要的东西:
print_r($_SESSION['cart'][0][0]);
echo "<br /><br />";
print_r($_SESSION['cart'][1][0][0]);
但如果超过2,我将无法获取阵列的其余部分。 以下是两个功能:
#Process
function ProcBuildCameo(){
global $session, $form;
$retval = $session->BuildCameo($_POST['product_type'], $_POST['product_format'], $_POST['setting_color'], $_POST['carving_color'], $_POST['line1'], $_POST['line2'], $_FILES['picture']['name']);
/* Submit order successful */
if($retval == 0){
$_SESSION['product_info'] = $session->product_info;
if(empty($_SESSION['cart'])){
$_SESSION['cart'] = $session->cart;
}
else{
array_push($_SESSION['cart'],$session->cart);
}
//var_dump($_SESSION['cart']);
//die();
$_SESSION['info'] = true;
$_SESSION['value_array'] = $_POST;
header("Location: view_cameo.php");
die();
}
/* Error found with form */
else if($retval == 1){
$_SESSION['value_array'] = $_POST;
$_SESSION['error_array'] = $form->GetErrorArray();
header("Location: cameo.php");
die();
}
}
#Session
function BuildCameo($subtype, $subformat, $subsetting, $subcarving, $subline1, $subline2, $subpicture){
global $mysql, $form;
$extra_cost = 0;
/* type error checking */
$field = "product_type";
if(!$subtype || strlen($subtype = trim($subtype)) == 0){
$form->SetError($field, "* Product not checked!");
}
/* format error checking */
$field = "product_format";
if(!$subformat || strlen($subformat = trim($subformat)) == 0){
$form->SetError($field, "* Format not checked!");
}
/* setting color error checking */
$field = "setting_color";
if(!$subsetting || strlen($subsetting = trim($subsetting)) == 0){
$form->SetError($field, "* Setting color not checked!");
}
/* carving color error checking */
$field = "carving_color";
if(!$subcarving || strlen($subcarving = trim($subcarving)) == 0){
$form->SetError($field, "* Carving color not checked!");
}
/* checks if line1 is empty */
if(!$subline1 || strlen($subline1 = trim($subline1)) == 0){
$subline1 = "N/A";
}
else{
$extra_cost = 15;
}
/* checks if line2 is empty */
if(!$subline2 || strlen($subline2 = trim($subline2)) == 0){
$subline2 = "N/A";
}
else{
$extra_cost = $extra_cost + 15;
}
$field = "picture";
$valid = array('jpg','jpeg');
if(!$subpicture || strlen($subpicture = trim($subpicture)) == 0){
$form->SetError($field, "* Select a picture to upload!");
}
if(!in_array(end(explode('.',$subpicture)),$valid)){
$form->SetError($field, "* Please upload jpg or jpeg files!");
}
/* Errors exist, have user correct them */
if($form->num_errors > 0){
return 1; //Errors with form
}
else{
if($mysql->GetProductInfo($subtype, $subformat, $subsetting)){
$this->product_info = $mysql->GetProductInfo($subtype, $subformat, $subsetting);
$subtotal = $this->product_info['price'] + $extra_cost;
array_push($this->product_info, $subline1, $subline2, $subcarving, $subpicture, $extra_cost, $subtotal);
if(!empty($this->cart)){
array_push($this->cart, $this->product_info);
}
else{
$this->cart = $this->product_info;
}
if(is_uploaded_file($_FILES["picture"]["tmp_name"])){
/**
* Give video file unique name
* and copy from temp folder
* to videos folder
*/
copy($_FILES["picture"]["tmp_name"],"pictures/" . $subpicture);
}
return 0;
}
}
}
答案 0 :(得分:1)
我相信你在这里混淆了你的嵌套数组。您需要一个包含一个或多个product_info数组的购物车数组。只需将调用放到array_push()并使用以下语法:
if(!empty($this->cart)){
// Add new product to array $this->cart
$this->cart[] = $this->product_info);
} else {
// Create Array with one product
$this->cart = array($this->product_info);
}
答案 1 :(得分:0)
您不需要像if(empty)那样的任何验证 - 这是PHP
$ this-&gt; cart [] = $ this-&gt; product_info;
所以但如果使用array_push,则必须执行此验证,因为array_push通过引用接受变量,无法创建或重新分配它。