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

时间:2015-01-10 17:29:55

标签: php arrays object mysqli

StackOverflow,这是第一个问题。我正在尝试用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]数组下的所有对象都保持相同的值。 (属性[2]的值成为属性[0]和属性[1]的值。)

我修改了代码,将每个对象的details->属性值推送到数组中,并按照我的喜好运行。只有在推动物体时我才有问题。

这是代码。     

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

(我的声誉不足以发布图片。我刚开始在这里。)

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

感谢阅读。

1 个答案:

答案 0 :(得分:0)

您最好将代码放在这里。

因为我使用你的链接我发现了这个:

$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);
      }
抱歉,暂时不能调试好,但是如果你说一切都很好 - 这应该有效。