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