douren5898 2011-08-29 20:05 采纳率: 100%
浏览 18
已采纳

数组with-in数组

I have been beating my head on my desk since 9am and with all the searching I did, I'm still not getting the results I need. What I have is an array that is dynamically created using.

  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;
  }

After being set the user can add another product or proceed to checkout. If they add another product I append the new array using array_push() as you can see. So now the array has more that 1 array. I want to be able to just grab the just two elements from each array. So I wrote this:

$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 />";      
        }
}

And this is what is outputted to the browser:

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

I can grab what I want if I use this:

print_r($_SESSION['cart'][0][0]);
echo "<br /><br />";
print_r($_SESSION['cart'][1][0][0]);

but I wont be able to grab the rest of the array if there is more than 2. Here are both functions:

#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;
        }
    }

}
  • 写回答

2条回答 默认 最新

  • dongyimo1293 2011-08-29 21:29
    关注

    I believe you mixed up your nested arrays here. You need one cart array that contains one or more arrays of product_info. Just drop the call to array_push() and use this syntax:

    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条)

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)