gpt4 book ai didi

php - OO + PHP + 数组 iSSUE

转载 作者:行者123 更新时间:2023-12-02 07:44:59 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com