Solved

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

Posted on 2002-03-22
4
250 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

773 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