php - OO + PHP + 数组 iSSUE

标签 php arrays

我真的不明白我数组的输出。一开始它看起来非常简单,但我已经在这个问题上花了几个小时了。看代码:

public function getAllProducts($limit){
$result = mysql_query("SELECT * FROM products ORDER BY RAND() LIMIT $limit") or die(mysql_error());
        $productArray = array();
        $count = 0;
        while($row = mysql_fetch_row($result)){
            $this->id = $row[0];
            $this->category_id = $row[1];
            $this->title = $row[2];
            $this->short_description = $row[3];
            $this->long_description = $row[4];
            $this->tag = $row[5];
            $this->price = $row[6];
            $this->weight = $row[7];
            $this->stock = $row[8];   
            $productArray[$count] = $this;
            $count++;
        }

            echo'<pre>';
            print_r($productArray);
            echo'</pre>';

        return $productArray;

    }

输出:

Array
(
    [0] => Product Object
        (
            [id] => 2
            [category_id] => 2
            [title] => Cart�o de Pascoa
            [short_description] => Short description bout this product 
            [long_description] => Long description about thi product but we don
            [tag] => Pascoa Coelho Cart�o Cartao Card
            [price] => 60,00
            [weight] => 1
            [stock] => 1
        )

    [1] => Product Object
        (
            [id] => 2
            [category_id] => 2
            [title] => Cart�o de Pascoa
            [short_description] => Short description bout this product 
            [long_description] => Long description about thi product but we don
            [tag] => Pascoa Coelho Cart�o Cartao Card
            [price] => 60,00
            [weight] => 1
            [stock] => 1
        )

)

现在,我将只对 print_r 函数做一个调整:

 echo'<pre>';
 print_r($productArray[0]);
 echo'</pre>';

新输出:

Product Object
(
    [id] => 1
    [category_id] => 1
    [title] => Cart�o de Natal
    [short_description] => Short descroption about this product
    [long_description] => Long description of this product. Nor used ri
    [tag] => Cart�o de Natal Natal Presente de Natal
    [price] => 55,00
    [weight] => 1
    [stock] => 1
)

再调整一下:

echo'<pre>';
print_r($productArray[1]);
echo'</pre>';

输出:

Product Object
(
    [id] => 2
    [category_id] => 2
    [title] => Cart�o de Pascoa
    [short_description] => Short description bout this product 
    [long_description] => Long description about thi product but we don
    [tag] => Pascoa Coelho Cart�o Cartao Card
    [price] => 60,00
    [weight] => 1
    [stock] => 1
)

数据库:

1   1   Cartão de Natal Short descroption about this product    Long description of this product. Nor used ri   Cartão de Natal Natal Presente de Natal 55,00   1   1

2   2   Cartão de Pascoa    Short description bout this product Long description about thi product but we don   Pascoa Coelho Cartão Cartao Card    60,00   1   1

您是否注意到,当我在 $productArray 中执行 print_r 或 var_dump 时,我们得到了错误的输出,而当我们执行 $productArray[0] 或 $productArray[1] 时,我们得到了正确的输出。有人注意到我的代码有问题吗?

非常感谢!

最佳答案

在 PHP5 中,将对象插入数组或传递给函数时不会复制对象。因此,您只是将一个对象多次插入到数组中,然后在每次循环迭代时覆盖其属性。

你应该:

1) 创建类的新实例:

    while($row = mysql_fetch_row($result)){
        $object = new self();
        $object->id = $row[0];
        $object->category_id = $row[1];

2) 或者在将对象插入数组时克隆该对象:

        $this->stock = $row[8];   
        $productArray[$count] = clone $this;
        $count++;
    }

关于php - OO + PHP + 数组 iSSUE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7720688/

相关文章:

PHP/MySQL - 如何证明用户编辑了值而不是管理员

PHP:自引用数组

c++ - C++中的 "for each"循环如何知道数组的长度

java - 有没有比使用数组更简单的拆分字符串的方法?

java - 将多个 java 属性值读取到字符串或数组中

php - 如何在 Laravel 中制作带有产品属性的智能过滤器

php - Ajax 响应出错

javascript - 简单JavaScript数组innerHtml中的php代码块

java - 将对象传递给 switch 语句以传递给不同类中的方法

php - 我的 CURL 脚本有问题