jrobles Posted December 23, 2009 Share Posted December 23, 2009 Im trying to put some information into an array but need some assistance. If i want to put the following info into the array how would I go about it? ProductID Qty PricePerUnit The array can contain one product or multiple. Each product will have the three fields i.e. ProductID = 1 Qty = 2 PricePerUnit = $4.00 ProductID = 2 Qty = 4 PricePerUnit = $2.76 ProductID = 3 Qty = 26 PricePerUnit = $4.34 etc... Link to comment https://forums.phpfreaks.com/topic/186189-array-help/ Share on other sites More sharing options...
Andy-H Posted December 23, 2009 Share Posted December 23, 2009 That really depends on how the data is currently stored and what you want to do with the data, looks like an object would come in handy in this situation to me. Again, depends what you want to do with the data. Do you have any code you can show?? Link to comment https://forums.phpfreaks.com/topic/186189-array-help/#findComment-983326 Share on other sites More sharing options...
trq Posted December 23, 2009 Share Posted December 23, 2009 You'll need a multidimensional array. One for each item, contained within one for all items. $items = array(); $items[] = array( 'ProductID' => 1, 'Qty' => 2, 'PricePerUnit' => 4.00 ); $items[] = array( 'ProductID' => 2, 'Qty' => 4, 'PricePerUnit' => 2.76 ); $items[] = array( 'ProductID' => 3, 'Qty' => 26, 'PricePerUnit' => 4.34 ); Link to comment https://forums.phpfreaks.com/topic/186189-array-help/#findComment-983327 Share on other sites More sharing options...
jonsjava Posted December 23, 2009 Share Posted December 23, 2009 a quick method (I'm sure there's a better way) is something like this: <?php $data = "ProductID = 1 Qty = 2 PricePerUnit = $4.00 ProductID = 2 Qty = 4 PricePerUnit = $2.76 ProductID = 3 Qty = 26 PricePerUnit = $4.34"; $data_array = explode("\n", $data); $count = 0; $new_array = array(); foreach ($data_array as $val){ $tmp_array = explode(" = ", $val); if ($tmp_array[0] == "ProductID"){ $count++; } $new_array[$count][$tmp_array[0]] = $tmp_array[1]; } print_r($new_array); If you want a full explanation/help understanding any/all parts of this, just ask. Link to comment https://forums.phpfreaks.com/topic/186189-array-help/#findComment-983329 Share on other sites More sharing options...
jrobles Posted December 23, 2009 Author Share Posted December 23, 2009 I eventually will be putting the array in a cookie where I can grab it from another page and insert the values into a mysql table as part of an "order detail" table. Jonsjava's code looks like what I need , how can I have to data in the $data var regardless of how many products I have. Jonsjava can you help me understand your code, i'm fairly new at PHP thanks a million! Link to comment https://forums.phpfreaks.com/topic/186189-array-help/#findComment-983335 Share on other sites More sharing options...
jonsjava Posted December 23, 2009 Share Posted December 23, 2009 basically, it takes a dump of data, that is formatted exactly like your input: ProductID = 1 Qty = 2 PricePerUnit = $4.00 ProductID = 2 Qty = 4 PricePerUnit = $2.76 ProductID = 3 Qty = 26 PricePerUnit = $4.34 and turns it into an array, based upon new lines. it then parses through each new array value, and splits up the value into a new array. It then checks the first bit to see if it's the first item in a new array. if it is, it auto-increments it to the next number. So, if you were working on array 0, and it sees "ProductID" it will begin array 1 and so on. Link to comment https://forums.phpfreaks.com/topic/186189-array-help/#findComment-983336 Share on other sites More sharing options...
jrobles Posted December 23, 2009 Author Share Posted December 23, 2009 sweet, I'll give that a shot and see what happens thanks a million! Happy Festivus! Link to comment https://forums.phpfreaks.com/topic/186189-array-help/#findComment-983338 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.