Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2002-03-22
4
Medium Priority
?
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
andriv earned 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
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.

721 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