在PHP中,有多種方式可以定義數組,包括:
$array = array("apple", "banana", "orange");
$array = ["apple", "banana", "orange"];
$array = array("name" => "John", "age" => 25, "city" => "New York");
$array = ["name" => "John", "age" => 25, "city" => "New York"];
$array = range(1, 10); // 創建一個包含1到10的整數數組
$string = "apple,banana,orange";
$array = explode(",", $string); // 將字符串拆分為數組
$name = "John";
$age = 25;
$city = "New York";
$array = compact("name", "age", "city"); // 創建一個關聯數組,鍵名為變量名,鍵值為變量值