Welcome to WebmasterWorld Guest from 54.166.102.61

Forum Moderators: coopster & jatar k

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)

WebmasterWorld Senior Member 10+ Year Member



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)

10+ Year Member



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

This code worked as expected for me:

<?php
$l = "a, b, c, d";
$r = explode(", ", $l);

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

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

WebmasterWorld Senior Member 10+ Year Member



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)

10+ Year Member



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)

10+ Year Member




I'd be curious to see a print_r of $row[image]
11:53 pm on Dec 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$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)

WebmasterWorld Senior Member 10+ Year Member



You might want to try something like this:


echo '<pre>image is
';
var_dump($image);
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)

10+ Year Member



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)

10+ Year Member



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...

-kpaul

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

10+ Year Member



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];

Vince

 

Featured Threads

Hot Threads This Week

Hot Threads This Month