Jump to content

Help with arrays and for loops, any help would be great!


lynx2003

Recommended Posts

 
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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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! 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.