Solved

what don't I understand about each() over arrays, nested wise?

Posted on 2002-03-22
4
243 Views
Last Modified: 2006-11-17
This code:

<?php
        $outsidearray = array("key1","key2");
        $insidearray = array("0","1");

            while(list(,$outerval) = each($outsidearray)){
                //$placeholder = $insidearray;
                while(list(,$innerval) = each($insidearray)){
                        print "inloop $innerval for $outerval<br>";
                }
        }
?>

only gets to key1, and then the outerloop stops.
However, if I uncomment that $placeholder line,
it iterates over key1 and key2

what am i missing about php looping??
0
Comment
Question by:kisrael
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
andriv earned 100 total points
ID: 6890062
it is not your loop that is the problem. I don't use the list and the each function but it seems as that the second use on list(...)=each(...) is killing the fist instance of it.  Whenever I use arrays I like the foreach loop because it will iterate through the loop for each element and you can utilize the key value and array value:

The following does the same thing you are trying to do with the example you provided.

<?
    $outsidearray = array("key1","key2");
       $insidearray = array("0","1");

//iterate through ousidearray
foreach($outsidearray AS $key => $outerval)
{
   //iterate through insidearray
     foreach($insidearray AS $key1 => $innerval)
     {
       print "inloop $innerval for $outerval<br>";
     }
}

?>
 
0
 

Author Comment

by:kisrael
ID: 6895146
Yeah, I actually have this now tagged as a bug at php.net... 'course, they don't see the bug as the (in my opinion) odd, side-effect heavy behavior of list()each() constructs, but as a documentation bug, either (my vote) taht list()each() isn't the same as foreach(), like the documentation says, or that reassignment I did resets that array pointer, even though its an RVALUE...sigh...I'm surprised people think this language is ready for primetime...
0
 
LVL 5

Expert Comment

by:andriv
ID: 6895163
Most people like it for it's processing speed and ease. But there are some issues that need to be addressed however, I'll take it over ASP any day.
0
 

Author Comment

by:kisrael
ID: 6895217
Yeah, I'm trying to think how it compares to JSP though, or Perl CGI.  I suspect it might be faster than non-modperl Perl, (plus Perl doesn't have that template option, just <<< style quoting).  For an experienced Java developer, though, maybe I'd best stick with JSPs. and Perl.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now