John84 Posted July 23, 2020 Share Posted July 23, 2020 (edited) Hi I have an array which looks a little like this, the first number in each array is a quantity $cars = array ( array(1,"BMW",15,13), array(8,"BMW",15,13), array(3,"Saab",5,2), array(4,"BMW",11,7), array(5,"Land Rover",17,15), array(6,"Saab",5,2) ); what I want to do is combine any array which matches another, but ignoring the quanitity value (first one). Then creating the array again and adding up the quanitity values and re-inserting them at the start. Therefore the end result would look like $cars = array ( array(9,"BMW",15,13), array(9,"Saab",5,2), array(4,"BMW",11,7), array(5,"Land Rover",17,15), ); Has anyone got any ideas on how I might do this? Thanks Edited July 23, 2020 by John84 Quote Link to comment Share on other sites More sharing options...
Barand Posted July 23, 2020 Share Posted July 23, 2020 Going back a step, how are you creating that original array? 1 Quote Link to comment Share on other sites More sharing options...
John84 Posted July 23, 2020 Author Share Posted July 23, 2020 (edited) 6 minutes ago, Barand said: Going back a step, how are you creating that original array? Hi, thanks for response. The original array is created from a bunch of posted input values, that looks something like <input type="text" name="cars[]" value="1,BMW,15,13"> <input type="text" name="cars[]" value="8,BMW,15,13"> <input type="text" name="cars[]" value="3,Saab,5,2"> Annoyingly I dont have access to change any of the script which produces those inputs, all i can do is post them to my script Edited July 23, 2020 by John84 Quote Link to comment Share on other sites More sharing options...
John84 Posted July 23, 2020 Author Share Posted July 23, 2020 Also, sometimes it isnt a alphanumeric value for the car, sometimes it's a numeric value such as <input type="text" name="cars[]" value="3,930,2,8"> <input type="text" name="cars[]" value="6,370,7,1"> Quote Link to comment Share on other sites More sharing options...
Barand Posted July 23, 2020 Share Posted July 23, 2020 Given that your form would look something like this your cars array would be an array of string values and not an array of arrays as you posted., IE $cars = [ 0 => "1,'BMW',15,13", 1 => "8,'BMW',15,13", 2 => "3,'Saab',5,2", 3 => "3,930,2,8", 4 => "6,370,7,1" ]; unless you are doing further processing to creat the posted array Quote Link to comment Share on other sites More sharing options...
John84 Posted July 23, 2020 Author Share Posted July 23, 2020 6 minutes ago, Barand said: Given that your form would look something like this your cars array would be an array of string values and not an array of arrays as you posted., IE $cars = [ 0 => "1,'BMW',15,13", 1 => "8,'BMW',15,13", 2 => "3,'Saab',5,2", 3 => "3,930,2,8", 4 => "6,370,7,1" ]; unless you are doing further processing to creat the posted array Yes very sorry, feeling a bit stupid now for my dumb example 😀 If I do a var_dump of the array then it comes out like 0 => string '1,930,9', 1 => string '2,370,3,4', 2 => string '6,930,9', 3 => string '3,930,2', 4 => string '8,370,2,1' so in that example, it would produce 0 => string '7,930,9', 1 => string '2,370,3,4', 2 => string '3,930,2', 3 => string '8,370,2,1' Quote Link to comment Share on other sites More sharing options...
chhorn Posted July 23, 2020 Share Posted July 23, 2020 you strpos() for the first comma, substr() the string into $quantity and $identifier, then create $countable[$identifier] and assign $quantity if not exists, or add up. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 23, 2020 Share Posted July 23, 2020 try $temp = []; foreach ($cars as $car) { $qty = intval($car); $key = trim(strstr($car, ','), ','); if (!isset($temp[$key])) $temp[$key] = 0; $temp[$key] += $qty; } foreach ($temp as $k => $t) { $newcars[] = "$t,$k"; } 1 1 Quote Link to comment Share on other sites More sharing options...
John84 Posted July 23, 2020 Author Share Posted July 23, 2020 12 minutes ago, Barand said: try $temp = []; foreach ($cars as $car) { $qty = intval($car); $key = trim(strstr($car, ','), ','); if (!isset($temp[$key])) $temp[$key] = 0; $temp[$key] += $qty; } foreach ($temp as $k => $t) { $newcars[] = "$t,$k"; } WOW! That looks superb! I'll give it a try then let you know, thank you Quote Link to comment Share on other sites More sharing options...
John84 Posted July 23, 2020 Author Share Posted July 23, 2020 2 hours ago, Barand said: try $temp = []; foreach ($cars as $car) { $qty = intval($car); $key = trim(strstr($car, ','), ','); if (!isset($temp[$key])) $temp[$key] = 0; $temp[$key] += $qty; } foreach ($temp as $k => $t) { $newcars[] = "$t,$k"; } Seems to work very well, you are a life saver, thank you for all the help given 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.