geudrik Posted November 17, 2009 Share Posted November 17, 2009 I know this is possable, but I'm not quite sure where to begin looking / coding What I need to achieve is this: I have a field in my database, that contains a list, chunked out in groups, using square brackets. The order is: [item][Quantity][item2][Quantity2] ... [Trees][300][boxes][4000][Gizmos][3] ==> Yeilds: 300 Trees, 4000 Boxes, 3 Gizmos When I retrieve that list, I need to iterate through each element, adding the item into an array, and it's corresponding quantity into another array (each chunk at the same index). Where should I start? PS: I know this isn't all that practical, I'm trying to use this as a learning exercise is all. Quote Link to comment https://forums.phpfreaks.com/topic/181907-iterate-through-items/ Share on other sites More sharing options...
genericnumber1 Posted November 17, 2009 Share Posted November 17, 2009 While I'd probably do it in a multidimensional array, if you insist on using two separate arrays.. Edit: Looks like you didn't want a solution, just a starting point. Look into explode and trim. A solution: <?php $itemStr = '[Trees][300][boxes][4000][Gizmos][3]'; $items = array(); $quantity = array(); $itemList = trim($itemStr, '[]'); $itemList = explode('][', $itemList); for($i = 0; $i < count($list); $i += 2) { $items = $itemList[$i]; $quantity = $itemList[$i+1]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/181907-iterate-through-items/#findComment-959438 Share on other sites More sharing options...
roopurt18 Posted November 17, 2009 Share Posted November 17, 2009 Why people store lists in databases like this is beyond me. Anyways: <?php $val = '[Trees][300][boxes][4000][Gizmos][3]'; // remove trailing [ and ] $val = trim( $val, '[]' ); $values = explode( '][', $val ); $max = count( $values ); for( $i = 0; $i < $max; $i += 2 ) { echo sprintf( "There are %s %s\n", $values[$i + 1], $values[$i] ); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/181907-iterate-through-items/#findComment-959439 Share on other sites More sharing options...
sasa Posted November 17, 2009 Share Posted November 17, 2009 or <?php $itemStr = '[Trees][300][boxes][4000][Gizmos][3]'; preg_match_all('/\[([^]]+)\]\[([^]]+)\]/',$itemStr,$out); print_r($out[1]); print_r($out[2]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/181907-iterate-through-items/#findComment-959477 Share on other sites More sharing options...
geudrik Posted November 17, 2009 Author Share Posted November 17, 2009 Well that was much easier than I had originally expected it to be... Alright, turning this whole idea upsidedown... How do I construct a table (well, multiple technically [the fewer the better]) that will allow for the following... Let's pretend we're making a game. In this game, there are blueprints. A blueprint is a set of instructions that allows a user to produce an item by using other ingame items. Example: To Create: Raven (ship): 11k Tritanium, 9k Pyerite, 2k Mexallon Golem (ship): 1 Raven, 1000 Morphite, 4 Sensor Clusters, 5 Propulsion Units, 7 Hardpoints Sensor Cluster (item): 300 Hypersynaptic Fibers, 90 methoflourine Plates, 8 morphite, 500 tritanium Propulsion Unit (item): 470 Tritanium, 4 construction blocks, 1 Exotic dancer (lewl?) Hardpoint (item): 7 crystaline carbide, 8 tungsten plating, 90 isogen, 30 zydrine Exotic Dancer (Commodity): Bought off market We're talking setting up a huge relational database here. It's totally doable, just a pain in the ass for the situation that it's going to be used in. Essentially, there are thousands of blueprints and items in the game, but only a small handful that the user will be concerned with (only the stuff they need to use). What's the most logical method of setting up relations here, if not simply comparing known data to what's being dumped from an ordered list? Edit: I suppose I should add in a purpose here... Hopefully, the goal here would be to have a quick access to a list of the owned blueprints for an individual player. When one is selected, a bunch of data is displayed from it (total minerals required among other). If raven is Tier 1 and Golem is Tier 2, I suppose a table for Tier 1, Tier 2, Components, and Minerals could be created. and a for loop could be used to assess the returned data from a T2 ship and just build up an array. Then math could be done... Yes? The issue with that though would potentially having a for each loop within a while, and if memory serves, PHP doesn't like that... Quote Link to comment https://forums.phpfreaks.com/topic/181907-iterate-through-items/#findComment-959483 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.