[Phpwm] foreach

Phil Beynon phil at infolinkelectronics.co.uk
Mon Dec 11 18:51:46 GMT 2006


> > Not quite sure I understand, but my best guess would be 'no'. is the
> > 'break' construct perhaps what you're after?
> >
> > http://uk.php.net/break
> >
>
>
> Alternatively, you could use while, next, each and do manual array
> iteration, e.g.
>
> while(!$found && list($key, $value) = each($thearray)) {
>      if($value == $whatever) {
>          $found = true;
>      }
>      next($thearray);
>      ....
> }
>
>
>
> As Jake suggested, break is far cleaner :
>
> foreach($myarray as $key => $value) {
>      if($value == $whatever) {
>           break;
>      }
>      ....
> }
>
>
> (My syntax may be wrong.... I've not checked it... )
>
> David.

I went for the break option, which seemed to be quite efficient and worked
just fine;

foreach($subdata as $nkey => $nvalue){
if($subdata[$nkey]['mident'] == "dummy_sub"){
foreach($rawdata as $rkey => $rvalue){
if($rawdata[$rkey]['main_index'] == $subdata[$nkey]['group_name']){
$subdata[$nkey]['mident'] = $rawdata[$rkey]['main_id'];
break;}
}}}

It's interesting that the PHP website defines a while() loop as the most
simplest, but I never seem to ever use them, hence this earlier was more a
practice / intellectual exercise to implement one in some code for a change.

Actually the bit of code above was mainly a bugfix for an automatic sitemap
generator for my content manager. As well as the main sitemap part I've
built another part which shows indexes and subindexes which don't currently
have content pages assigned. The bug came about in that it wasn't assigning
a record ident to the main index in the unassigned indexes area when there
was already another subindex which did have pages in the main sitemap
display. Took a while to figure that one out!

The workling demo of the sitemap, with the above code included, is at
(http://www.bortec-labs.com/tabview/tabview.php) if anyone wants to check it
out.
Unless any more bugs appear then that's the finished version.
It's now got a cookie based page / status positional system built in and is
highly optimised.

All comments are appreciated.

Phil







More information about the Phpwm mailing list