ltd17 Posted March 26, 2010 Share Posted March 26, 2010 Hi all, I'm relatively new to PHP dev, and am working with a client with a very specific set of requirements (one being "no database"...ugh). The setup: I've got a flat file (.txt) that I pull in to a template.php file. The template.php turns the .txt's data into an array, and assigns each segment of the array a variable ( ie : $pagename = substr($data[17], 0 , -1); ). Then it creates/displays the page based on those variables. The problem is that the .txt files are all going to be different lengths, and they all end in a repeating list of "products" (this is for a gallery, of sorts). Each "product" has twelve rows of data (so, twelve lines in the text file -- Product Name, Product Link, Product Image, Product Owner, etc), and what I need to do is grab that ending list and break it out into its own associative two-dimensional array (a parent array of a variable number of children associative arrays -- the "products" -- that each have a dozen chunks). So, here's what I've got: if ($type == "multi") { $pagename = substr($data[17], 0 , -1); $loopcount = substr($data[16], 0 , -1); for ($counter = 0; $counter < $loopcount; $counter += 1) { $proditerate = 18 + ($counter * 12); $multiproducts = array( array( ProductTitle => substr($data[$proditerate], 0 , -1), ProductLink => substr($data[$proditerate+1], 0 , -1), ProductImage => substr($data[$proditerate+2], 0 , -1), ShopkeeperImage => substr($data[$proditerate+3], 0 , -1), ShopkeeperID => substr($data[$proditerate+4], 0 , -1), ShopName => substr($data[$proditerate+5], 0 , -1), ShopLink => substr($data[$proditerate+6], 0 , -1), ShopkeeperName => substr($data[$proditerate+7], 0 , -1), DollarPrice => substr($data[$proditerate+8], 0 , -1), CentsPrice => substr($data[$proditerate+9], 0 , -1), CommentExpression => substr($data[$proditerate+10], 0 , -1), CommentQuote => substr($data[$proditerate+11], 0 , -1), ), ); }; The system itself is all done -- I know where the items start in the .txt file, I know how to roll through and grab each line and stick it in an associative array, etc. The problem is that the above obviously overwrites the parent array on each rotation (so I end up with a parent array containing one associative child array). How do I do this? What follows is what I'd like to have happen, but it breaks (no loops in a 2D array definition, apparently) -- any ideas on how to set this up? Here's what I'd LIKE to have happen: if ($type == "multi") { $pagename = substr($data[17], 0 , -1); $loopcount = substr($data[16], 0 , -1); $multiproducts = array( for ($counter = 0; $counter < $loopcount; $counter += 1) { $proditerate = 18 + ($counter * 12); array( ProductTitle => substr($data[$proditerate], 0 , -1), ProductLink => substr($data[$proditerate+1], 0 , -1), ProductImage => substr($data[$proditerate+2], 0 , -1), ShopkeeperImage => substr($data[$proditerate+3], 0 , -1), ShopkeeperID => substr($data[$proditerate+4], 0 , -1), ShopName => substr($data[$proditerate+5], 0 , -1), ShopLink => substr($data[$proditerate+6], 0 , -1), ShopkeeperName => substr($data[$proditerate+7], 0 , -1), DollarPrice => substr($data[$proditerate+8], 0 , -1), CentsPrice => substr($data[$proditerate+9], 0 , -1), CommentExpression => substr($data[$proditerate+10], 0 , -1), CommentQuote => substr($data[$proditerate+11], 0 , -1), ), } ); Any help is greatly, greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/196643-loop-through-a-section-of-one-array-to-create-a-2d-associative-array/ Share on other sites More sharing options...
ltd17 Posted March 26, 2010 Author Share Posted March 26, 2010 Hm...I'm left to wonder if silence means "that's hard" or "you're an idiot"... any insights, even general pointers, would be killer... Quote Link to comment https://forums.phpfreaks.com/topic/196643-loop-through-a-section-of-one-array-to-create-a-2d-associative-array/#findComment-1032486 Share on other sites More sharing options...
DavidAM Posted March 26, 2010 Share Posted March 26, 2010 $multi[] = array(...); will add the array() on the right-hand side as a new element of the $mult array. It will start at zero if the array is not already defined, or will add the element with a numeric key greater than the last (adding 1 each time). so you end up with: $multi[0] => array( ... ) $multi[1] => array( ... ) and so forth. Just add the square brackets to the assignment you currently have and drop the outer array. Also, I usually define the variable as an empty array just to make the code clearer on what I am doing. It is not actually necessary: $multiproducts = array(); for ($counter = 0; $counter < $loopcount; $counter += 1) { $proditerate = 18 + ($counter * 12); $multiproducts[] = array( ProductTitle => substr($data[$proditerate], 0 , -1), ProductLink => substr($data[$proditerate+1], 0 , -1), ProductImage => substr($data[$proditerate+2], 0 , -1), ShopkeeperImage => substr($data[$proditerate+3], 0 , -1), ShopkeeperID => substr($data[$proditerate+4], 0 , -1), ShopName => substr($data[$proditerate+5], 0 , -1), ShopLink => substr($data[$proditerate+6], 0 , -1), ShopkeeperName => substr($data[$proditerate+7], 0 , -1), DollarPrice => substr($data[$proditerate+8], 0 , -1), CentsPrice => substr($data[$proditerate+9], 0 , -1), CommentExpression => substr($data[$proditerate+10], 0 , -1), CommentQuote => substr($data[$proditerate+11], 0 , -1), ); }; Quote Link to comment https://forums.phpfreaks.com/topic/196643-loop-through-a-section-of-one-array-to-create-a-2d-associative-array/#findComment-1032536 Share on other sites More sharing options...
ltd17 Posted March 27, 2010 Author Share Posted March 27, 2010 Thanks a lot for the reply. I actually came to pretty much the same conclusion after a couple hours of tinkering -- I realized I was completely overlooking just appending each chunk to the parent array...long week. Regardless, much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/196643-loop-through-a-section-of-one-array-to-create-a-2d-associative-array/#findComment-1032556 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.