PHP foreach help
- Started
- Last post
- 4 Responses
- ESKEMA
I want to place info gotten from a foreach before the foreach to avoid using 2 separates foreach.
simplified code below:< header > echo $pages-Stuff-Y < / header >
< ul >
foreach $pages
< li > $pages->stuffX < / li >
add $pages->stuffY to $pagesStuffY
end foreach
< / ul >instead of this:
< header >
foreach $pages
add $pages->stuffY to $pagesStuffY
end foreachecho $pagesStuffY
< / header >
< ul >
foreach $pages
< li > $pages->stuffX < / li >
add $pages->stuffY to $pagesStuffY
end foreach
< / ul >Is this dumb and I should just use 2 foreach or is there a simple and elegant solution?
The info being processed is still quite a bit, and avoid going through it a second time would be great.
- drgs0
you could generate a string of code in the first foreach to echo it later
- ESKEMA0
yeah, but the big part of the code is the list, if it were the other way around it wouldn't be a problem, but echoig 300+ items with lots of info and images seems worse than a second foreach no?
- drgs0
you can maybe get away with array_reduce or array_map in the header if you dont want to use foreach