views:

61

answers:

6

Hi I have a method that returns an array of support tickets. Each support ticket can have many notes so I have a method that returns an array of tickets notes with that ticket id. I want to display the notes alongside the ticket which would mean nesting the get notes inside the foreach loop.

foreach($tickets as $ticket){
     //display ticket info

     //now get ticket notes using method getNotes()

     foreach($ticketnote as $note){
         //display note
     }
}   

Do nested loops like this have performance implications? Is this good practice?

Thanks, Jonesy

+1  A: 

Well, the program does what you tell it to do. It will pass through each note of all the tickets. If that has to be done - it has to be done. So far a better practice doesn't exist if you have to loop through all of them. The only performance implication is more iterations than it would be without the nested loop, but without the nested loop you'd get no results.

Alexander
+2  A: 

It's not a problem.

Do nested loops like this have performance implications? Is this good practice?

Nested loops have no specific performance implications.

But of course, a lot of data may be processed; depending on how much it is, you could reach memory or performance limits. But that is a given, and would also occur if you would use a different control structure instead of a nested loop.

An array-/foreach()-based solution will always require loading the full data set into memory before it starts processing.

If you are fetching data from a database, you could consider re-structuring your functions so they fetch and process a database record one by one instead of loading them all into an array, and foreaching over them. That allows you to process data sets that are larger than your script's memory limit.

Pekka
thanks for replying! Do I have to destroy the note object after it's used in the foreach loop? The reason I'm asking is I'm getting is the second time it's used I get 'Trying to get property of non-object in..'
iamjonesy
@Jonesy both impossible to answer without seeing some actual code, but usually no: The new function call would overwrite the old object.
Pekka
A: 

Definitely not a good practice but i've never found a better solution than this.

fabrik
+1  A: 

There are only performance implications if you have excessive numbers of tickets and notes. So, if you had 1000 tickets and each had 1000 notes, the inner loop would run c. 1,000,000 times. But, as others have said, if it's necessary to do it this way then it's necessary.

Skilldrick
A: 

On first observation, since you would be displaying the 'ticket notes' anyway, it wouldn't matter much whether you do this in a single loop or two nested loops. The net iteration count would be same.

On presentation issue, it's not wise to display all this information at once. You would certainly want to apply some kind of pagination.

Gunner
+1  A: 

Others have already pointed you in the right direction.

However, another aproach which has yet to be mentioned, and worth(possibly?) looking into are the Spl Iterators

$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));

foreach($iter as $key=>$value) {
    echo $key.' =>'.$value;
}
Russell Dias
what does it give? How is it superior to regular approach? What advantages does it give?
Alexander
**1.** Thats a vague question. What it can give you is a method by which to iterate through a multidimensional array. **2.** I never said it was superior, I merely stated that it was a 'another approach'. I have not done any benchmarks to compare. **3.** Cleaner code (although arguable) as nested foreach loops can get very messy.
Russell Dias