Php 将对象附加到while循环中的数组会覆盖以前的元素
StackOverflow,这里的第一个问题。我正在尝试用MySQL学习PHP,但我在数组和对象方面遇到了问题 以下是我希望发生的事情Php 将对象附加到while循环中的数组会覆盖以前的元素,php,arrays,object,mysqli,Php,Arrays,Object,Mysqli,StackOverflow,这里的第一个问题。我正在尝试用MySQL学习PHP,但我在数组和对象方面遇到了问题 以下是我希望发生的事情 Category object { [details] => Array ( [category] => Desktop [notes] => Lorem ipsum dolor sit amet, consectetur adipisicing elit. ) [properties] =&g
Category object {
[details] => Array (
[category] => Desktop
[notes] => Lorem ipsum dolor sit amet, consectetur adipisicing elit.
)
[properties] => Array (
[0] => Property Object (
[details] => Array (
[property] => Hard Disk Size
[category] => Desktop
[notes] =>
)
)
[1] => Property Object (
[details] => Array (
[property] => Memory Frequency
[category] => Desktop
[notes] =>
)
)
[2] => Property Object (
[details] => Array (
[property] => Video Card Graphics Adapter
[category] => Desktop
[notes] =>
)
)
)
但结果是[properties]数组下的所有对象都具有相同的值。
(属性[2]的值变为属性[0]和属性[1]的值。)
我修改了代码,将每个对象的details->property值推送到数组中,它按照我所希望的方式工作。只有在推物体时,我才有问题
这是密码。
class Category {
public static final function generate_template() {
return array(
"category" => null,
"notes" => null
);
}
private static function query_category( $input_string ) {
global $connection;
$stmt = $connection->prepare(
"SELECT * FROM categories WHERE category = ? LIMIT 1"
);
$stmt->bind_param( "s", $input_string );
if ( !$stmt->execute() ) return false;
$category_details = Category::generate_template();
$stmt->bind_result(
$category_details["category"],
$category_details["notes"]
);
$stmt->fetch();
return $category_details;
}
private static function query_category_properties( $category ) {
global $connection;
$stmt = $connection->prepare(
"SELECT * FROM properties WHERE category = ?"
);
$stmt->bind_param( "s", $category );
if ( !$stmt->execute() ) return false;
$property_details = Property::generate_template();
$stmt->bind_result(
$property_details["property"],
$property_details["category"],
$property_details["notes"]
);
$properties_array;
while ( $stmt->fetch() ) {
$properties_array[] = new Property( $property_details );
// This caused the problem.
// It works as expected when written like this.
// $properties_array[] = (new Property( $property_details ))->details["property"];
}
return $properties_array;
}
// Object Things
public $details;
public $properties = null;
public function __construct( $category ) {
$this->details = Category::query_category( $category );
$this->properties = Category::query_category_properties( $category );
}
}
class Property {
public static final function generate_template() {
return array(
"property" => null,
"category" => null,
"notes" => null
);
}
public $details = array();
public function __construct( $property_details ) {
$this->details = $property_details;
}
}
$category = new Category( "Desktop" );
print_r( $category );
?>
下面是代码的作用。
下面是存储代码属性的作用。
(我的声誉不足以发布图片。我只是从这里开始的。)
我想做的是将这些对象存储在一个数组中,但它似乎不起作用。
谢谢阅读。您最好将代码放在这里 因为以你的链接为例,我发现:
$properties_array;
while ( $stmt->fetch() ) {
$properties_array[] = 1; //(new Property( $property_details ))->details["property"];
}
我认为它工作得很好,除了-它是你真正想要调试的部分。(也许我错了)
但我想一定是:
$stmt->bind_result(
$property_name,
$property_category,
$property_notes
);
$properties_array = array();
while ( $stmt->fetch() ) {
$properties_array[] = array($property_name,$property_category, $property_notes);
}
很抱歉,目前无法进行更好的调试,但如果您说一切正常,那么应该可以了。您可以发布代码吗?请在此发布相关代码并将代码添加到正文中。很抱歉,我复制了另一份脚本,但我忘了我正在处理我在此处发布的脚本。你说得对。这就是我想要调试的。您发布的更正就是导致该问题的原因。注释部分只是提醒我自己,如果我使用字符串,而不是对象,它会像预期的那样工作。那么你能在你的问题正文中发布你的真实代码吗?现在解释一下怎么了?刚刚发布了代码。错误的是,当我将这些对象推入数组时,得到的只是同一对象的多个实例。(最后一个对象在某种程度上被复制到了数组的前几个元素上。)我感到困惑的是,当我创建一个字符串数组时,它可以正常工作。我得到一系列不同的东西。当我创建一个对象数组时,一切都是一样的。哦,我想我能理解发生了什么。:-)检查我的答案更新。尝试将引用发送到数组元素。这导致了致命错误。(致命错误:第44行的C:\Users\Jason\Google Drive\Project Vigilum\includes\functions\categorization.php中的调用时间传递引用已被删除)