在PHP中,可以通過以下方式定義對象數組:
$obj1 = new stdClass();
$obj1->name = "John";
$obj1->age = 25;
$obj2 = new stdClass();
$obj2->name = "Jane";
$obj2->age = 30;
$arr = array($obj1, $obj2);
$obj1 = new stdClass();
$obj1->name = "John";
$obj1->age = 25;
$obj2 = new stdClass();
$obj2->name = "Jane";
$obj2->age = 30;
$arr = [$obj1, $obj2];
$obj1 = new stdClass();
$obj1->name = "John";
$obj1->age = 25;
$obj2 = new stdClass();
$obj2->name = "Jane";
$obj2->age = 30;
$arr = [$obj1, $obj2];
這樣就定義了一個包含對象的數組。你可以通過索引訪問數組中的對象,并使用箭頭運算符(->)訪問對象的屬性。