mainstreetop Posted March 11, 2011 Share Posted March 11, 2011 I have the following multidimensional array where each sub-array represents quantity[0] and item #[1]: Array ( [0] => Array ( [0] => 1 [1] => 12113 ) [1] => Array ( [0] => 2 [1] => 21200 ) [2] => Array ( [0] => 4 [1] => 10200 ) [3] => Array ( [0] => 1 [1] => yyyy ) [4] => Array ( [0] => 20 [1] => xxxxx ) [5] => Array ( [0] => 5 [1] => 21200 ) I'm trying to consolidate like items. For example I need to create a new array that reflects any duplicate part numbers with a combined quantity. (ie. ... [0] => 7 [1] => 21200 ... Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted March 11, 2011 Share Posted March 11, 2011 Can you use code tags so that it's easier to read please Quote Link to comment Share on other sites More sharing options...
mainstreetop Posted March 11, 2011 Author Share Posted March 11, 2011 @ huggiebear - Sorry about that and thanks for responding. This is somewhat new to me. Hopefully, this looks a little better. [0] Array => 0 ( [0] => 1 [1] => 12113 ) [1] => Array ( [0] => 2 [1] => 21200 ) [2] => Array ( [0] => 4 [1] => 10200 ) [3] => Array ( [0] => 1 [1] => yyyy ) [4] => Array ( [0] => 20 [1] => xxxxx ) [5] => Array ( [0] => 5 [1] => 21200 ) Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted March 11, 2011 Share Posted March 11, 2011 How is this array constructed? Is it provided for you like that, or have you built it up like that? Quote Link to comment Share on other sites More sharing options...
mainstreetop Posted March 11, 2011 Author Share Posted March 11, 2011 I created it like that. The idea is to grab quantities and item numbers entered in an order form. So, I created an array of quantities and an array of item numbers. I then created array containing both sub-arrays. Now, I want to validate the item numbers against the database and insert the data into the 'cart' table. However, I'm trying to consolidate like items in the vent the user enters the same item number more than once. Hopefully, that made sense. Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted March 11, 2011 Share Posted March 11, 2011 It makes perfect sense, but I'm sure there's a better way to do it. Here's an interim solution: <?php // Declare totals as an array $totals = array(); // This is the format of your array (I assume posted from your form) notice some duplicates $submitted[] = array('1', '12113'); $submitted[] = array('2', '21200'); $submitted[] = array('4', '10200'); $submitted[] = array('1', 'yyyy'); $submitted[] = array('20', 'xxxxx'); $submitted[] = array('5', '21200'); $submitted[] = array('7', '21200'); // This sticks it into a new array keyed on product id with quantity as a value foreach ($submitted as $k => $a){ if (!array_key_exists($submitted[$k][1], $totals)){ $totals[$submitted[$k][1]] = $submitted[$k][0]; } else { $totals[$submitted[$k][1]] = $totals[$submitted[$k][1]] + $submitted[$k][0]; } } // Print the array and total quantities echo '<pre>'; print_r($totals); echo '</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
mainstreetop Posted March 11, 2011 Author Share Posted March 11, 2011 Thank you HuggieBear. And, you are right... I'm sure there is a more efficient way. I did not realize I could simply use the '+' operator to achieve what I need. I'm going to work on simplifying the entire piece. Thanks again. I appreciate the help. Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted March 11, 2011 Share Posted March 11, 2011 Nothing wrong with your solution, but the key really isn't necessary for the example provided (hmm, pet peeve, maybe.) <?php $submitted = array( array('1', '12113'), array('2', '21200'), array('4', '10200'), array('1', 'yyyy'), array('20', 'xxxxx'), array('5', '21200'), array('7', '21200') ); $finalArr = array(); foreach ($submitted as $dataArr){ $finalArr["{$dataArr['1']}"] = (isset($finalArr["{$dataArr['1']}"])) ? $finalArr["{$dataArr['1']}"] + $dataArr[0] : $dataArr[0]; } ?> Quote Link to comment Share on other sites More sharing options...
mainstreetop Posted March 11, 2011 Author Share Posted March 11, 2011 I thought the additional key was necessary. If I were to create a single array using quantity => item# as the $k => $v, then the array will rearrange itself because of duplicate keys if the user enters the same quantity for two or more items. Or, maybe I do not fully understand which key is superfluous. 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.