aebstract Posted February 3, 2009 Share Posted February 3, 2009 I've tried a few different things, I'm getting more comfortable with arrays, but.. $_SESSION['cart'] = array($specialid => '1', 'mild steel'); All this will do is make my array like this: Array ( [7] => 1 [8] => mild steel ) Somehow I want more values for [7] without creating 8, since I had to reserve all of the keys for ids. Trying to make it possible something like: Array ( [7] => '1', 'mild steel', 'etc' [8] => '2', 'stainless steel' ) Also I'm sure there might be a special way to go about getting the extra values, if something like this is possible? Possibly inside the foreach loop. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted February 3, 2009 Share Posted February 3, 2009 Sounds like you're after a multidimensional array. In essence, each element of the array is an array itself. Consider something like this: $array[0] = array('foo','bar'); $array[1] = array('blah'); $array[2] = array(array(1,2,3),4,5); echo '<pre>.print_r($array,1).'</pre>'; As you'll see by the last element, there's no reason why we're limited to 2 dimensions Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 Cool, tried out this: $_SESSION['cart'] = array($specialid => array('1', 'mild steel')); which easily gave me: Array ( [7] => Array ( [0] => 1 [1] => mild steel ) ) Which, I'm gonna hope all I have to do is run two loops to grab all the information. Thanks Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 Is this method: foreach ($_SESSION['cart'] as $id => $array2){ print "item: $id<br />"; foreach ($array2 as $row => $item){ if ($row == 0){ print "Qty: $item<br />"; } if ($row == 1){ print "Material: $item<br />"; } } } The best for getting and using the items? I don't think an if statement should be necessary, but I'm not entirely sure. This resulted with: item: 7 Qty: 1 Material: mild steel Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 Using: $_SESSION['cart'][$id][mildsteel] = array(qty => 1, size => 4.5); I get this array: Array ( [8] => Array ( [mildsteel] => Array ( [qty] => 1 [size] => 4.5 ) [stainless] => Array ( [qty] => 1 [size] => 4.5 ) ) ) Now I am trying to control it so that I can display the cart correctly, which for this let's say there's a table displaying each item. For each material of each item I need to show a new row. The foreach I have below will not work correctly. foreach ($_SESSION['cart'] as $id){ foreach ($id as $material){ foreach ($material as $row){ print "ID: $id, Meterial: $material, Qty: $row[0], Size: $row[1]"; } } } Basically need to somehow use that array to do something like this: ID: 8, Material: mildsteel, QTY: 1, Size, 4.5 ID: 8, Material: stainless, QTY: 1, Size, 4.5 and for it to be able to do this for every item ID and each different Material under each id. Hope this makes sense, and I'm still playing with it to see if I can control it. Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 I think I am getting closer with this: foreach ($_SESSION['cart'] as $id => $material){ foreach ($material as $row){ print "ID: $id, Meterial: $material, Qty: $row[0], Size: $row[1]"; } } but still nope, that's showing: ID: 8, Meterial: Array, Qty: , Size: ID: 8, Meterial: Array, Qty: , Size: Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 Noone has a clue as to how I can control these elements? Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 Well I got this: foreach ($_SESSION['cart'] as $id => $array2){ foreach ($array2 as $material => $array3){ foreach ($array3 as $key => $key2){ echo "$id $material $key $key2<br />"; } } } Which turns out to be: 8 mildsteel qty 1 8 mildsteel size 4.5 8 stainless qty 1 8 stainless size 4.5 Which is obviously not right, need it to do something like 8 mildsteel qty 1 size 4.5 8 stainless qty 1 size 4.5 Still trying to get it under control though, anyone? Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 Sigh, now I'm to this: foreach ($_SESSION['cart'] as $id => $array2){ foreach ($array2 as $material => $array3){ echo "$id $material "; foreach ($array3 as $key => $key2){ echo " $key2"; } echo "<br />"; } } which displays 8 mildsteel 1 4.5 8 stainless 1 4.5 but I obviously still can't control my quantity and size in to their own variables.. just a general $key2, not sure how to break it down better. Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 Now, foreach ($_SESSION['cart'] as $id => $array2){ foreach ($array2 as $material => $array3){ echo "$id $material "; foreach ($array3 as $key => $key2){ if ($key == qty) { echo "QUANTITY: $key2"; } else { echo " $key: $key2"; } } echo "<br />"; } } Results: 8 mildsteel QUANTITY: 1 size: 4.5 8 stainless QUANTITY: 1 size: 4.5 Though I think I should still have some sort of more control over it than I am having right now? I know SOMEONE has a solution to this! edit: changed the inside foreach, foreach ($_SESSION['cart'] as $id => $array2){ foreach ($array2 as $material => $array3){ echo "$id $material "; foreach ($array3 as $key => $key2){ if ($key == qty) { $qty = $key2; } if ($key == size) { $size = $key2; } } echo "$size $qty<br />"; } } So that gives me a slight bit more flexibility but I am almost sure there has got to be a way to do this without using the if statements? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted February 3, 2009 Share Posted February 3, 2009 Assuming you're still working with your data like this: Array ( [8] => Array ( [mildsteel] => Array ( [qty] => 1 [size] => 4.5 ) [stainless] => Array ( [qty] => 1 [size] => 4.5 ) ) ) The following should work for you: <?php foreach($array as $id => $materials){ foreach($materials as $material => $details){ echo "ID: $id Material: $material "; foreach($details as $detail => $value){ echo "$detail: $value"; } echo '<br />'; } } ?> Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 That does work, however I need to be able to control the values a little better. For instance, the quantity needs to be able to go in to a textbox and the size will be displayed somewhere else. Is there a solid way for me to control this information better? Quote Link to comment Share on other sites More sharing options...
aebstract Posted February 3, 2009 Author Share Posted February 3, 2009 Wooo I finally got it with this: foreach ($_SESSION['cart'] as $id => $array2){ foreach ($array2 as $material => $array3){ echo "$id $material "; echo "{$array3['size']} {$array3['qty']}<br />"; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.