Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

829 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