Jump to content

Multidimensional Array Help


thomashw

Recommended Posts

I have a multidimensional array using sessions.

 

The array consists last array in the multidimensional array has a 0 and 1 "key" but it's only displaying the value for the 0 key, and doesn't display the value corresponding to the 1 key.

 

$j = 0;
foreach($_SESSION['cart'][$pid]["$j"] as $key => $value)
{
  echo "$value, ";
  $j++;
}

 

I'm also new to this, so this may be a stupid question.

Link to comment
Share on other sites

Array ( [cart] => Array ( [2] => Array ( [productid] => 2 [quantity] => 1 [price] => 299.99 [0] => Array ( [option0] => Bulb Type: H1 ) [1] => Array ( [option1] => Color Temperature: 4300K ) ) ) )

 

When I use the for loop, it only displays the Bulb Type: H1, and not the Color Temperature: 4300K.

 

Thanks!

Link to comment
Share on other sites

If you are getting all this info from a database, wouldn't it be easier to just add a item # and quantity to the cart? When a user views the shopping cart or goes to check out, you could query your database for the details. A properly designed database of items would not have the same part number with options like:

 

part 1, blue

part 1, green

part 1, pink

 

instead you would have:

 

part 1

part 2

part 3

Link to comment
Share on other sites

The options are selected by the person on the site, and each part has multiple options to be selected, so I need to "remember" what they selected... there are also multiple categories for options, so if I entered all of the different ways the options could be selected, I'd have thousands for just one part.

Link to comment
Share on other sites

Wow, that makes it so much more organized!

 

Array
(
    [cart] => Array
        (
            [2] => Array
                (
                    [productid] => 2
                    [quantity] => 1
                    [price] => 299.99
                    [0] => Array
                        (
                            [option0] => Bulb Type: H1
                        )

                    [1] => Array
                        (
                            [option1] => Color Temperature: 4300K
                        )

                )

        )

)

 

Now that I see it like this, the options don't need to be in new arrays. It'd be great to have the options in the $pid array, but the $pid array is created higher in the code. How can I "add" the options to the end of the $pid array instead of creating a new array for the options? I realize I need to remove the "[]" from the end of this code, but how do I just "add" to the $pid array?

 

$i = 0;
while(isset($_POST["option$i"]))
  {
    // Add the options to the array
    $_SESSION['cart'][$pid][] = array("option$i" => $_POST["option$i"]);
    $i++;
  }

Link to comment
Share on other sites

$_SESSION['cart'][2][] = 'some option';

 

will add 'some option' to the product in the array shown.

 

I would create an options array inside the product array, that way you can loop through the options when outputting:

 

$pid = 1;
$_SESSION['cart']["$pid"]['options'][] = 'chrome';
$_SESSION['cart']["$pid"]['options'][] = 'gold plated';
$_SESSION['cart']["$pid"]['options'][] = 'diamond encrusted';

 

check the array with print_r(), and you will see that they have been added to the product options array

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.