s_ainley87 Posted April 27, 2008 Share Posted April 27, 2008 Hello Everyone, I have built session cart but I am issues iterating through it and getting the data from it I need, basically I need from the array the catergory name, product name, price and quantity but have no idea how to do it :-(, the array looks like this, Array ( [cart] => Array ( [57] => Array ( [quantity] => 1 [price] => 249.99 ) [74] => Array ( [quantity] => 1 [price] => 69.99 ) ) ) how would I get all this data from the array using a foreach, I have no idea how to form it :-(. Please Help Thanks s_ainley87 Link to comment https://forums.phpfreaks.com/topic/103142-iteration-issues/ Share on other sites More sharing options...
Fadion Posted April 27, 2008 Share Posted April 27, 2008 As u have a multidimensional array, ull need nested foreach() loops to go through each array level. Take a look at the following example: <?php $cart = array('cart' => array(57 => array('quantity' => 1, 'price' => 249.99), 74 => array('quantity' => 1, 'price' => 69.99))); foreach($cart as $Cval){ //loop through the 'cart' values foreach($Cval as $Pkey => $Pval){ //loop through array(57..... echo 'This index: ' . $Pkey . ' has these values<br />'; foreach($Pval as $key => $val){ //loop through array('quantity'..... echo $key . ': ' . $val . '<br />'; } echo '<br />'; } } ?> Link to comment https://forums.phpfreaks.com/topic/103142-iteration-issues/#findComment-528343 Share on other sites More sharing options...
s_ainley87 Posted April 27, 2008 Author Share Posted April 27, 2008 thankyou, however theses values $cart = array('cart' => array(57 => array('quantity' => 1, 'price' => 249.99), 74 => array('quantity' => 1, 'price' => 69.99))); will not also be the same how can i resolve that? Link to comment https://forums.phpfreaks.com/topic/103142-iteration-issues/#findComment-528348 Share on other sites More sharing options...
Fadion Posted April 27, 2008 Share Posted April 27, 2008 The actual values doesnt matter as long as the array has the stated structure. Otherwise u have to modify the script, by adding/removing a foreach loop, basing on the depth of the array. Guess ure not meaning that the array will have dynamic depth as it would need a recursive approach. Link to comment https://forums.phpfreaks.com/topic/103142-iteration-issues/#findComment-528355 Share on other sites More sharing options...
s_ainley87 Posted April 27, 2008 Author Share Posted April 27, 2008 I am sorry i dont really understand the ins outs of it, I am new to this PHP lark. What i need it to do is loop through the cart regarless and of what what is held in the cart and how many products are in it, I then need to some how get those values and hold then in hidden form fields. Link to comment https://forums.phpfreaks.com/topic/103142-iteration-issues/#findComment-528359 Share on other sites More sharing options...
Fadion Posted April 27, 2008 Share Posted April 27, 2008 Look, if u have this array: $cart = array('cart' => array(57 => array('quantity' => 1, 'price' => 249.99), 74 => array('quantity' => 1, 'price' => 69.99))); or this array: $cart = array('cart' => array(57 => array('quantity' => 1, 'price' => 249.99), 74 => array('quantity' => 1, 'price' => 69.99))); 24 => array('quantity' => 2, 'price' => 49.99))); 18 => array('quantity' => 1, 'price' => 54.99))); 63 => array('quantity' => 3, 'price' => 144.99))); The script will still work. Assuming those 57, 74 etc are the ids of the products, guess that array structure will do the work. To pass those values in hidden inputs, consider the following but modify it for your needs: <?php echo "<form name='myForm' method='post' action='mypage.php'>"; $cart = array('cart' => array(57 => array('quantity' => 1, 'price' => 249.99), 74 => array('quantity' => 1, 'price' => 69.99))); foreach($cart as $Cval){ foreach($Cval as $Pkey => $Pval){ foreach($Pval as $key => $val){ echo "<input type='hidden' name='" . $Pkey . "' value='" . $val . "' />"; } } } echo '</form>'; ?> Link to comment https://forums.phpfreaks.com/topic/103142-iteration-issues/#findComment-528377 Share on other sites More sharing options...
s_ainley87 Posted April 27, 2008 Author Share Posted April 27, 2008 i appreciate your help, but I cannot predict what product the user will choose so how can I decide what products numbers to give the array? Link to comment https://forums.phpfreaks.com/topic/103142-iteration-issues/#findComment-528390 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.