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

发布于 2024-11-07 19:02:34 字数 1825 浏览 0 评论 0原文

我正在尝试在上传后导入制表符分隔的文件。其核心内容是通过以下函数完成的。我正在尝试构建一个类实例数组。代码如下:

导入函数

$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;
}

数组输出

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

已知信息细分

  • 最终数组长度/大小是正确的。 (应该是文件中的行 - 2)

  • productImport 类中有多少属性并不特别重要,只要它等于文件中每行的相同数量的选项卡即可读。

  • importList 函数回显 $cells[$i] 的正确值,这些值与我在数组输出中缺少的值相同。

问题似乎是未将值分配给属性或未读取属性。我不确定为什么会出现这种情况,但我认为这是因为 PHP 不是我的主要语言,并且 foreach 循环可能很明显;)

我正在使用 PHP v5.2.6

这段代码有什么问题吗?

答案:

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伪装你 2024-11-14 19:02:34

我认为问题出在这一部分:

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++;
        }

根据 php 手册除非引用了该数组,否则 foreach 将对指定数组的副本进行操作,而不是对数组本身进行操作。因此您分配的值将在循环后被丢弃。

编辑: 除此之外,您正在循环访问对象属性,尽管 手册没有明确说明它,看来你需要foreach($class as $key => $value)而不仅仅是foreach($class作为$值)

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)

囍孤女 2024-11-14 19:02:34

在 foreach 循环中,分配的变量(例如 $ProductProperty)不是引用,因此它们实际上不会影响循环之外的任何内容。

$ProductProperty = $cells[$i] 仅影响当前迭代。

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.

南…巷孤猫 2024-11-14 19:02:34

除了其他人所说的之外,您似乎每次都尝试将属性数据插入到同一个对象中,因为您没有在循环中创建任何新的 ProductImport 实例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文