一个PHP对象数组[关闭]

I want to populate an array with objects..

my class:

class MyOrderClass {
public $id;
public $many;
public $cost;

public function __construct($id, $many, $cost) {

    $this->id = $id;
    $this->many = $many;
    $this->cost = $cost;

    }
}

and i want to have something like this :

$orders[0]->id = 1;
$orders[0]->many = 1;
$orders[0]->cost = 100;

can you help me ?

$orders = [];
$orders[] = new MyOrderClass(1, 1, 100);
$orders[] = new MyOrderClass(1, 2, 150);

// alternatively
array_push($orders, new MyOrderClass(1, 3, 200));

// contents of the array
var_dump($orders);

You just need to push the objects into an array

you're saying you want to make an array from a class but you're code example is creating an array of objects so, if you want what your example is see the other answer, if you want what you actually asked for, you can just cast the object as array:

$order = new MyOrderClass(1, 1, 100);
$order = (array) $order;
var_dump($order);

https://3v4l.org/LF2ps

You need to assign the first element of the array to be an instance of your class/object, and then change the properties:

$orders = [];
$orders[0] = new MyOrderClass();

$orders[0]->id = 1;
$orders[0]->many = 1;
$orders[0]->cost = 100;