homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

FOREACH acting funny.
print_r shows output should be values but I'm getting more arrays

 8:12 pm on Dec 24, 2004 (gmt 0)

Assigning values to an array from a list like this:

$image = explode(", ", $row[image]);

So I get this:

Array ( [0] => foo.jpg )

But when I do this:

foreach ($image as $v)
echo $v;

The value of $v is also an array.

Doing a print_r on $v gives me:

Array ( [0] => foo.jpg [1] => 0 )

when it should just be foo.jpg.

What's going on here?



 8:43 pm on Dec 24, 2004 (gmt 0)

Perhaps it has something to do with the variable your passing to explode().

This code worked as expected for me:
$l = "a, b, c, d";
$r = explode(", ", $l);

foreach($r as $v)
echo $v. "<br>";


 8:50 pm on Dec 24, 2004 (gmt 0)

I don't think so. As you see, the array that's produced by explode looks OK. Somehow $v is becoming an array... I've used foreach a lot in the past but this is the first time it's given me this strange problem.


 9:34 pm on Dec 24, 2004 (gmt 0)

Are you running PHP 4.3.10? They mucked about in the internals of foreach in the most recent release.

I've had similar problems in previous versions of PHP, in my case always brought on by either incorrect or overly aggressive use of aliasing elsewhere in the code. The interpreter seems to get kicked into wacky mode and things break all over.


 9:35 pm on Dec 24, 2004 (gmt 0)

I'd be curious to see a print_r of $row[image]


 11:53 pm on Dec 24, 2004 (gmt 0)

$row[image] is just a field from the database, just a list of images such as "foo.jpg" or "foo1.jpg, foo2.jpg, foo3.jpg". I'm using explode then foreach to display each image in the resulting array. I've just never had a problem like this before.


 8:28 am on Dec 25, 2004 (gmt 0)

You might want to try something like this:

echo '<pre>image is
foreach ($image as $k => $v){
echo '
key is '.$k. and v is '.var_dump($v);
echo '</pre>';

var_dump() gives you a little more info than print_r (its format is just more irritating), but on the off chance its output could be revealing, it's probably worth a try. Sure hope there isn't a problem with the new foreach. Jolly, what kind of 'aggressive aliasing' or what was that do you mean, what was it for you that broke foreach?


 8:13 pm on Dec 25, 2004 (gmt 0)

Sadly, I don't have any surviving code examples. What's gotten me in trouble in the past is a combination of loops, complex multidimensional array manipulation & construction, using aliasing extensively to emulate references, and forgetting that PHP aliases are not references nor are they scoped to the enclosing block.

Typically when I've messed this up it takes quite a bit of debugging, as symptoms manifest far from the cause. I've definitely seen symptoms where what goes in isn't what comes out, like in the foreach problem we're talking about here.


 8:29 pm on Dec 27, 2004 (gmt 0)

it might be php 4.3.10 and an older zendoptimizer. when i upgraded to php 4.3.10, i had the same thing happen to me. i'd contact your host...



 1:26 pm on Jan 14, 2005 (gmt 0)

We're starting to get reports back from our users about this. It normally starts "Our host just upgraded PHP and now . . . ". It seems to happen from 4.3.8 and upwards but I can't seem to recreate it on Windows. I'm going to get a linux box up and running to see if I can recreate it there but would be interested to see if others are experiencing the same thing with foreach. As a temp workaround I'm doing . . .
if(is_array($objValue)) $objValue = $objValue[0];


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved