lynx2003 Posted June 2, 2014 Share Posted June 2, 2014 Hey guys, I'm looking for some help using php foreach loops with a php array, basically my loop goes through each array key foreach ($products as $key => $product) { However I don't want the loop to restart until the $product ['product_description'] changes, until then I just want it to keep importing the information as that's what my code is doing, only when the product description changes do I want it to restart the foreach loop, My issue however is I'm not sure how to make it go onto the next array key without restarting the loop as I want all of the keys with the same $product ['product_description'] to keep importing to the same place before the loop restarts as you see I'm very inexperienced using php for loops with arrays, so any help at all would be very much appreciated! Thank-you very much in advance! Quote Link to comment Share on other sites More sharing options...
Sinan Posted June 2, 2014 Share Posted June 2, 2014 Could you tell what exactly you are trying to do? What's your goal? Because what you are asking is very unusual since you can't stop a loop from "restarting". You can select keys individually however. Like so: $product_description = $products[0]['product_description']; But I don't know how your array looks like and I am not clear on what you are trying to do. Quote Link to comment Share on other sites More sharing options...
lynx2003 Posted June 2, 2014 Author Share Posted June 2, 2014 Well basically my loop writes information to a txt file, the information is stored in an array, however the information in the txt file is seperated by a line break. My aim is to write all of the data in the array with the same $product ['product_description'] into the file, then add the line break Quote Link to comment Share on other sites More sharing options...
lynx2003 Posted June 2, 2014 Author Share Posted June 2, 2014 however I'm not really sure how to do that, My idea was to create a regular foreach loop and then try to write all of the ones with the same description in before the loop restarts and the line break is added, any help would be amazing! also thank-you very much for your fast reply! Quote Link to comment Share on other sites More sharing options...
Barand Posted June 2, 2014 Share Posted June 2, 2014 As already asked, what does your array look like? echo "<pre>", print_r($products, 1), "</pre>"; Also How are you building the array in the first place? What should your output look like? Quote Link to comment Share on other sites More sharing options...
Sinan Posted June 2, 2014 Share Posted June 2, 2014 You don't need to do that if your array is setup correctly with each product having its' own unique key. May I see your array? Grouping by description is not recommended I would recommend to group by id. Off-topic: I think you should take a look at SQL it could make things a lot easier for you. Quote Link to comment Share on other sites More sharing options...
lynx2003 Posted June 2, 2014 Author Share Posted June 2, 2014 I'm afraid I can't really print my array, and I didn't create the code that it came from so I can't really tell you exactly how it's created as it comes from a lot of different sources, I've tried to figure it out my main issue now is just making a piece of code that takes all of the arrays with the same description key and writes them, however it could even echo them, that would work too, I'm just not sure how to only select the ones with the same description before it restarts the loop to find ones with other descriptions, :\ any help would be great, thanks for your advice up to now Quote Link to comment Share on other sites More sharing options...
Sinan Posted June 2, 2014 Share Posted June 2, 2014 We have too little information to help you out it's like asking to chop a tree without having a tree. We need to see some code. Quote Link to comment Share on other sites More sharing options...
maxxd Posted June 2, 2014 Share Posted June 2, 2014 Try this in your script print("<pre>".print_r($products,true)."</pre>"); and post the output here. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 2, 2014 Share Posted June 2, 2014 As you are giving us no information all I can do is wish you good luck and say goodbye. 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.