php 如何在php中创建对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20174228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:57:09  来源:igfitidea点击:

How to create array of objects in php

phparraysoopobject

提问by Alex Miles

I'm attempting to create an array of objects in php and was curious how I would go about that. Any help would be great, thanks!

我正在尝试在 php 中创建一个对象数组,并且很好奇我将如何去做。任何帮助都会很棒,谢谢!

Here is the class that will be contained in the array

这是将包含在数组中的类

<?php

class hoteldetails {
private $hotelinfo;
private $price;

public function sethotelinfo($hotelinfo){
    $this->hotelinfo=$hotelinfo;
}

public function setprice($price){
    $this->price=$price;
}

public function gethotelinfo(){
    return $hotelinfo;
}

public function getprice(){
    return $price;
}

}

And here is what I am attempting to do-

这就是我正在尝试做的-

<?PHP
include 'file.php';

$hotelsdetail=array();    

$hotelsdetail[0]=new hoteldetails();
$hotelsdetail[0].sethotelinfo($rs);
$hotelsdetail[0].setprice('150');


?>

The class attempting to create the array doesn't compile but is just a best guess as to how I can do this. Thanks again

尝试创建数组的类不会编译,但只是我如何做到这一点的最佳猜测。再次感谢

回答by slhck

What you should probably do is:

你可能应该做的是:

$hotelsDetail = array();

$details = new HotelDetails();
$details->setHotelInfo($rs);
$details->setPrice('150');

// assign it to the array here; you don't need the [0] index then
$hotelsDetail[] = $details;

In your specific case, the issue is that you should use ->, not .. The period isn't used in PHP to access attributes or methods of a class:

在您的具体情况下,问题是您应该使用->,而不是.. PHP 中不使用句点来访问类的属性或方法:

$hotelsdetail[0] = new hoteldetails();
$hotelsdetail[0]->sethotelinfo($rs);
$hotelsdetail[0]->setprice('150'); 

Note that I capitalized the class, object, and function names properly. Writing everything in lowercase is not considered good style.

请注意,我正确地将类、对象和函数名称大写。以小写形式编写所有内容不被认为是好的风格。

As a side note, why is your price a string? It should be a number, really, if you ever want to do proper calculations with it.

作为旁注,为什么你的价格是一个字符串?它应该是一个数字,真的,如果你想用它做适当的计算。

回答by Bryant Hymanson

You should append to your array, not assign to index zero.

您应该附加到您的数组,而不是分配给索引零。

$hotelsdetail = array();    
$hotelsdetail[] = new hoteldetails();

This will append the object to the end of the array.

这会将对象附加到数组的末尾。

$hotelsdetail = array();    
$hotelsdetail[] = new hoteldetails();
$hotelsdetail[] = new hoteldetails();
$hotelsdetail[] = new hoteldetails();

This would create an array with three objects, appending each one successively.

这将创建一个包含三个对象的数组,依次附加每个对象。



Additionally, to correctly access an objects properties, you should use the ->operator.

此外,要正确访问对象属性,您应该使用->运算符。

$hotelsdetail[0]->sethotelinfo($rs);
$hotelsdetail[0]->setprice('150');

回答by Bhupendra Jha

You can get the array of object by encoding it into json and decoding it with $assoc flag as FALSE in json_decode() function.

您可以通过将对象数组编码为 json 并在 json_decode() 函数中使用 $assoc 标志将其解码为 FALSE 来获取对象数组。

See the following example:

请参阅以下示例:

    $attachment_ids = array();
    $attachment_ids[0]['attach_id'] = 'test';
    $attachment_ids[1]['attach_id'] = 'test1';
    $attachment_ids[2]['attach_id'] = 'test2';
    $attachment_ids                 = json_encode($attachment_ids);
    $attachment_ids                 = json_decode($attachment_ids, FALSE);
    print_r($attachment_ids);

It would render an array of objects.

它会渲染一个对象数组。

output:

输出:

Array
(
[0] => stdClass Object
    (
        [attach_id] => test
    )

[1] => stdClass Object
    (
        [attach_id] => test1
    )

[2] => stdClass Object
    (
        [attach_id] => test2
    )

)