Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print_r and foreach disagree...

Status
Not open for further replies.

gagz

Programmer
Nov 21, 2002
333
US
I have the following code in a script:

Code:
print "<pre>";
print_r ($landscape);
print "</pre>";

foreach($landscape as $myval)
{
print $myval."<br>";
}

print_r prints the contents of the array correctly, but the foreach loop prints out "Array" for the valr of $myval each time... what am I doing wrong? This script had been working for 2 years until he provided updated php to version 4.3.10.
 
i know, but i don't... Here's the output:
Code:
Array
(
    [0] => ma-2004-18.jpg
    [1] => ma-2004-19.jpg
    [2] => ma-2004-21.jpg
    [3] => ma-2004-22.jpg
    [4] => ma-2004-23.jpg
    [5] => ma-2004-24.jpg
    [6] => ma-2004-26.jpg
    [7] => ma-2004-27.jpg
    [8] => ma-2004-29.jpg
    [9] => ma-2004-30.jpg
    [10] => ma-2004-40.jpg
    [11] => ma-2004-38.jpg
)

Array
Array
Array
Array
Array
Array
Array
Array
Array
Array
Array
Array

$myval is referenced in no other places. and like i said, this script *never* changed when it stopped working... it hasn't been changed since early 2003.
 
That is odd. It seems to me like the newer version of PHP is treating your strings as an array. I'm afraid I don't know enough about PHP's internals or the differences between versions to really speculate.
 
Here's the whole thing:

Code:
<?
include("header.html");
?>
<?

# program specific text
include($type.$year.".inc");

$portrait = array();
$landscape = array();

if ($dir = @opendir("./")) {
  while (($file = readdir($dir)) !== false) {
    if (ereg("(jpg)\$",$file) && ereg("^$type",$file) && ereg("$year",$file))
    {
       $imagesize = getimagesize("./".$file);
       #print $file . "<br>";
       # figure out landscape/portrait,for arrays
       $wth = intval($imagesize[0]);
       $hgt = intval($imagesize[1]);
       if (intval($hgt) > intval($wth))
       {
           array_push($portrait, $file);
       }
       else
       {
           array_push($landscape, $file);
       }
    }
  }
  closedir($dir);
}
print "<pre>";
print_r ($landscape);
print "</pre>";
# rifle through arrays, grouping jpg orientations together.
foreach($landscape as $myval)
{
print $myval."<br>";
       #$imagesize = GetImageSize("./".$myval);
       $wth = intval($imagesize[0]);
       $hgt = intval($imagesize[1]);
       $factor = 150;
       $mult = $wth / $factor;
       $phgt = $hgt / $mult;
       $pwth = $wth / $mult;
       #print "<a href='$myval'><img src='$myval' width=$pwth height=$phgt border=0></a>\n";
}

foreach($portrait as $myval)
{
#print "$myval<br>";
       #$imagesize = GetImageSize("./".$myval);
       $wth = intval($imagesize[0]);
       $hgt = intval($imagesize[1]);
       $factor = 150;
       $mult = $hgt / $factor;
       $phgt = $hgt / $mult;
       $pwth = $wth / $mult;
       #print "<a href='$myval'><img src='$myval' width=$pwth height=$phgt border=0></a>\n";
}

include("footer.html");
?>

i had originally thought it was getimagesize that was broken, but i tested that, and its giving the correct results...
 
could be somenting wrong with readdir(),
or array_push()

try a
while (($file = readdir($dir)) !== false)
{
echo gettype(file) ;
...
}

enetually see what'shappend after array_push();

strange, anyway...




___
____
 
that's just messed up. When i do gettype, it calls it a string. however, in the foreach loop, if instead i print $myval[0], it works... whats up with that? How does it go from one to the other, with no intermediary steps? I guess i could live with it this way, but i'd really like to know what changed to make this act this way...
 
haven't followed all of this thread. wouldn't it work if you changed the foreach loop so it reads:

Code:
echo "<table>";
foreach($landscape as $mykey=>$myval)
{
  echo "<tr><td>$mykey</td><td>$myval</td></tr>";
}
echo "</table>";
 
This problem was recently mentioned in the comp.lang.php newsgroup. The solution mentioned was to reinstall the Zend Optimizer (or upgrade it).

Talk to your hosting provider.

Ken
 
Code:
foreach (array_keys($landscape) as $myval)
{
    print $myval."<br>";
    print $landscape[$myval]."<br>";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top