Solved

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses how to create an extensible mechanism for linked drop downs.
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 …

747 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

13 Experts available now in Live!

Get 1:1 Help Now