在PHP中,有三種常見的方式來定義數組:
$numbers = array(1, 2, 3, 4, 5);
$person = array("name" => "John", "age" => 25, "country" => "USA");
$students = array(
array("name" => "John", "age" => 20, "country" => "USA"),
array("name" => "Emma", "age" => 22, "country" => "UK"),
array("name" => "Luisa", "age" => 21, "country" => "Spain")
);
此外,還可以使用[]
語法來定義數組:
$fruits = ["apple", "banana", "orange"];
這是PHP 5.4版本及更高版本引入的簡潔語法。