rajaroy Posted March 4, 2011 Share Posted March 4, 2011 I am currently building (for my first time) a shoppingcart. Its an multidimensional array in a session that looks like this. Array ( [23] => Array ( [0] => 23 [1] => 11.32.097. [2] => 635 [3] => 2 [4] => 89 ) [800] => Array ( [0] => 800 [1] => 20.40.017. [2] => 100 [3] => 1 [4] => 125 ) ) The 4th key contains the price value. What i am trying to do is get all the sub-array's 4th key value and count them together to get a total price. I tried doing this with a foreach loop but its not my strong point, sadly. I you have some time can you please take a look how to solve this? Thanks in advance, Roy Quote Link to comment https://forums.phpfreaks.com/topic/229557-total-price-from-multidimensional-array/ Share on other sites More sharing options...
wepnop Posted March 4, 2011 Share Posted March 4, 2011 $total_price = 0; for ($i=0; $i<=count($array)-1; $i++) { $total_price += $array[$i][4]; } Quote Link to comment https://forums.phpfreaks.com/topic/229557-total-price-from-multidimensional-array/#findComment-1182688 Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 I am currently building (for my first time) a shoppingcart. Its an multidimensional array in a session that looks like this. .... I could be wrong, but something about this stikes me as fundamentally flawed... Quote Link to comment https://forums.phpfreaks.com/topic/229557-total-price-from-multidimensional-array/#findComment-1182689 Share on other sites More sharing options...
rajaroy Posted March 4, 2011 Author Share Posted March 4, 2011 @Muddy_Funster Sorry if I pronounced the sentence wrong. I tried my best to explain my problem, but if its not understandable i can try to explain again. @wepnop I tried the code but i get a total of 0. I only changed $array to my array name. Dont know if you had noticed, but the sub-array's keys are the id's of the product. Could it be that the for loop is having difficulties with it when counting ++? Quote Link to comment https://forums.phpfreaks.com/topic/229557-total-price-from-multidimensional-array/#findComment-1182691 Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 It's not a problem with the explination, it's that you are writing a shopingcart to run through session variables stored in the PHP. That just stikes me as the wrong way to do things. I would have had it run from a database, but that's just me. I think you probably want a foreach(): $total = 0; foreach ($array as $key => $value) { $total = $total + $value; } but I could be way off, I don't use arrays too much. Quote Link to comment https://forums.phpfreaks.com/topic/229557-total-price-from-multidimensional-array/#findComment-1182695 Share on other sites More sharing options...
wepnop Posted March 4, 2011 Share Posted March 4, 2011 @Muddy_Funster Sorry if I pronounced the sentence wrong. I tried my best to explain my problem, but if its not understandable i can try to explain again. @wepnop I tried the code but i get a total of 0. I only changed $array to my array name. Dont know if you had noticed, but the sub-array's keys are the id's of the product. Could it be that the for loop is having difficulties with it when counting ++? For what i know, php values in arrays can be accesed using keys or the natural position in they where filled in, so, that have to work, i think. You can use the foreach example also, i wrote a for because you say you get stuck with it. $total = 0; foreach ($array as $key => $value) { $total += $value[4]; } Quote Link to comment https://forums.phpfreaks.com/topic/229557-total-price-from-multidimensional-array/#findComment-1182697 Share on other sites More sharing options...
rajaroy Posted March 4, 2011 Author Share Posted March 4, 2011 @wepnop I really really suck at loopings cause for some reason it doenst make sense to me. But the foreach worked fabulously, thanks @Muddy_Funster My first tought was also to use database for shoppingcarts but i thought of the folowing problems that i couldn't think a solution for. First the user that visits the website is a guest. When he/she is adding products to his/her cart then it would be inserted into database. But if this guest leave's the website, the cartdata would still be in the database, causing to fill up unnecessarily. How would you empty the cartdata? I couldn't think of any solutions, maybe you can . Quote Link to comment https://forums.phpfreaks.com/topic/229557-total-price-from-multidimensional-array/#findComment-1182703 Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 To be fair, you shouldn't really let guests add items to a cart anyway - if they want a cart they should have to log in. However, you could run a cronjob on the database that clears out any cart tables that have not been accessed for more than 24 hours. Quote Link to comment https://forums.phpfreaks.com/topic/229557-total-price-from-multidimensional-array/#findComment-1182707 Share on other sites More sharing options...
rajaroy Posted March 4, 2011 Author Share Posted March 4, 2011 @Muddy_Funster I do agree with you that guest should not be able to create a cart without registering first, unfortunatly the person for whom i am making the website wants it this way. Dont really know much about cronjobs, but i definitely am going to take a look at it for future shoppingcarts. I really am greatfull for your help and information, thanks again. Greetings, Roy Quote Link to comment https://forums.phpfreaks.com/topic/229557-total-price-from-multidimensional-array/#findComment-1182715 Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 No problem Roy. ... I do agree with you that guest should not be able to create a cart without registering first, unfortunatly the person for whom i am making the website wants it this way. ... There are a good amount of people here who can associate quite vividly with this kind of scenario. If you've not been there already check out http://clientsfromhell.net/ Quote Link to comment https://forums.phpfreaks.com/topic/229557-total-price-from-multidimensional-array/#findComment-1182719 Share on other sites More sharing options...
rajaroy Posted March 4, 2011 Author Share Posted March 4, 2011 Haha great website. Quote Link to comment https://forums.phpfreaks.com/topic/229557-total-price-from-multidimensional-array/#findComment-1182727 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.