Array with-in an array
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, $for开发者_StackOverflow中文版m;
$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;
}
}
}
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);
}
You do not need any verification like if (empty) - this is PHP
$this->cart[] = $this->product_info;
so but if you use array_push you must do this verification because array_push accept variable by reference and can't create or reassign it.
精彩评论