Using foreach loops

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Using foreach loops

Post by Stryks »

Just wondering about calling the foreach function.

Lets say I have an array with repeated values and I wanted to run a loop for each individual version. I could do something like ...

Code: Select all

$arr_ing = array_unique($ing_list);
foreach($ing_list as $key=>$item) {
   // do something
}
... but I'm wondering if there are going to be problems with something like ...

Code: Select all

foreach(array_unique($ing_list) as $key=>$item) {
   // do something
}
I guess what I'm really worried about is that it will run the array_unique on each pass.

Any help would be great. Cheers
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Re: Using foreach loops

Post by patrikG »

Stryks wrote:I guess what I'm really worried about is that it will run the array_unique on each pass.
Yup. And you only need to run it once.
Post Reply