【问题标题】:Appending objects to an array in a while loop overwrites previous elements在 while 循环中将对象附加到数组会覆盖以前的元素
【发布时间】:2015-01-10 17:29:55
【问题描述】:

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

?>

这就是代码的作用。 https://lh5.googleusercontent.com/uSsYNLzRIRfQihsSBrPeYylkcQ965Vs9TjyyIYz32Mf8suAO4y52w5JkMMMaj9D02pqEWVp9Rxs=w1332-h521

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

(我的声誉还不足以发布图片。我只是从这里开始。)

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

感谢阅读。

【问题讨论】:

  • 你能把代码贴出来吗?
  • 请在这里发布相关代码
  • 将代码添加到主体中。

标签: php arrays object mysqli


【解决方案1】:

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

因为例如使用您的链接,我发现了这个:

$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 中删除了调用时传递引用)
猜你喜欢
  • 2021-04-17
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 2020-08-27
  • 2018-03-15
  • 2012-10-02
  • 2021-04-06
相关资源
最近更新 更多