在 while 循环中将对象附加到数组会覆盖以前的元素

Appending objects to an array in a while loop overwrites previous elements

Whosebug,第一个问题在这里。我正在尝试用 MySQL 学习 PHP,但我遇到了数组和对象的问题。

这是我希望发生的事情。

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] 数组下的所有对象都具有相同的值。 (properties[2] 的值变成了 properties[0] 和 properties[1] 的值。)

我修改了代码以将每个对象的详细信息->属性 值推送到数组中,它按我希望的方式工作。只有在推物体的时候我有问题。

这是代码。

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 );

?>

代码的作用如下。 https://lh5.googleusercontent.com/uSsYNLzRIRfQihsSBrPeYylkcQ965Vs9TjyyIYz32Mf8suAO4y52w5JkMMMaj9D02pqEWVp9Rxs=w1332-h521

这是存储代码属性的作用。 https://lh5.googleusercontent.com/xe5N2U6rGOlr3x4vh_Qm0CFRS8-7pDryn9FuSldyPXN5huRhBy31bqH1d7H4AS8phDcfunyp9OE=w1332-h521

(我的声望不够post张图片,我才刚开始。)

我想做的是将这些对象存储在一个数组中,但它似乎不起作用。

感谢阅读。

你最好把你的代码放在这里。

因为使用你的 link 例如我发现了这个:

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

抱歉,目前无法更好地进行调试,但如果您说一切正常 - 应该可以。