php 中定義數組的關鍵字是 array,用于創建和初始化一個包含鍵值對的數組。可使用 array() 或方括號 [] 定義數組,數組的鍵可以是字符串或數字,元素可以是任何數據類型,并且數組是動態的,可以根據需要增長或縮小。
PHP 中數組的定義關鍵字
PHP 中定義數組的關鍵字是 array
。它用于創建和初始化一個數組,并將元素存儲在鍵值對中。
語法:
<code class="php">$array_name = array( 'key1' => 'value1', 'key2' => 'value2', ... );</code>
登錄后復制
示例:
<code class="php">$fruits = array( 'apple' => 'red', 'banana' => 'yellow', 'orange' => 'orange' );</code>
登錄后復制
這個示例創建了一個名為 fruits
的數組,其中包含鍵值對:
‘apple’ => ‘red’
‘banana’ => ‘yellow’
‘orange’ => ‘orange’
數組也可以使用方括號語法定義:
<code class="php">$colors = ['red', 'blue', 'green'];</code>
登錄后復制
注意:
數組的鍵可以是字符串或數字。
數組元素可以是任何數據類型。
數組是動態的,這意味著它們可以根據需要增長或縮小。