为什么这个PHP代码不输出属性值?

I am trying to import a tab delimited file after upload. The meat of this is done with the following function. I'm trying to build an array of class instances. The code follows:

Import Function

$AddedProducts;
function importList($filename)
{
    global $AddedProducts;
    $AddedProducts=array();
    $fileHandle = fopen($filename, "r");
    $currentProduct = new productImport();

      $line=fgets($fileHandle); $line=fgets($fileHandle); //throw away top 2 lines
    echo '<hr>';
    while(true)
    {
        $line = fgets($fileHandle);
        if($line == null)   break;

        $cells=explode('    ', $line);
        $i=0;

        foreach($currentProduct as $ProductProperty)
        {
            if(isset($cells[$i]))
            {
                $ProductProperty = $cells[$i];
                echo $i . '. ' . $cells[$i] . "<br>";
            }
            else return false;
            $i++;
        }
        echo "<hr>";
        $AddedProducts[]=$currentProduct;
    }
    fclose($fileHandle);
    return true;
}

Array Output

<?  
$i=0;
foreach($AddedProducts as $AddedProduct)
{
    $i++;
    echo "<hr>" . $i . "<br>";
    foreach($AddedProduct as $key=>$value)
    {
        echo $key . ' = ' . $value . '<br>';
    }
}
?> 

Breakdown of Known Info

  • The final array length/size is correct. (Should be lines in file - 2)

  • It doesn't particularly matter how many properties are in the productImport class so long as it equates to the same number of tabs per line in the file being read.

  • importList function echos proper values for $cells[$i] which are the same values I'm missing in the array output.

The problem seems to be either the values aren't being assigned to the properties or the properties are not being read. I'm not sure of why either would be the case but I assume it is because PHP is not my primary language and likely something obvious about the foreach loops ;)

I'm using PHP v5.2.6

What's wrong with this code?

Answer:

foreach($currentProduct as $ProductProperty) becomes
foreach($currentProduct as &$ProductProperty)

I think the problem is in this section:

foreach($currentProduct as $ProductProperty)
        {
            if(isset($cells[$i]))
            {
                $ProductProperty = $cells[$i];        /* this seems to be the problem */
                echo $i . '. ' . $cells[$i] . "<br>";
            }
            else return false;
            $i++;
        }

According to the php manual, Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. so the value you assign is discarded after the loop.

Edit: Apart from that, you are looping through object properties and although the manual does not explicitly state it, it seems you need foreach($class as $key => $value) instead of just foreach($class as $value)

In your foreach loops, the assigned variables such as $ProductProperty are not references, therefore they will not actually affect anything outside the loop.

i.e. $ProductProperty = $cells[$i] only affects the current iteration.

In addition to what the others are saying, it seems that you are attempting to insert property data to the same object every time, since you're not creating any new productImport instances in the loop.