PHP 中的數(shù)組實(shí)際上是一個(gè)有序圖。圖是一種把 values 映射到 keys 的類型。此類型在很多方面做了優(yōu)化,因此你可以把它當(dāng)成真正的數(shù)組來使用,或列表(矢量),散列表(是圖的一種實(shí)現(xiàn)),字典,集合,棧,隊(duì)列以及更多可能 性。因?yàn)榭梢杂昧硪粋€(gè) PHP 數(shù)組作為值,也可以很容易地模擬樹。解釋這些結(jié)構(gòu)超出了本手冊的范圍,但對于每種結(jié)構(gòu)你至少會發(fā)現(xiàn)一個(gè)例子。要得到這些結(jié)構(gòu)的更多信息,我們建議你參考有 關(guān)此廣闊主題的外部著作。
AD:
實(shí)用函數(shù)
有相當(dāng)多的實(shí)用函數(shù)作用于數(shù)組,參見數(shù)組函數(shù)庫一節(jié)。
注: unset() 函數(shù)允許取消一個(gè)數(shù)組中的鍵名。要注意數(shù)組將不會重建索引。
'one', 2 => 'two', 3 => 'three' ); unset( $a[2] ); /* 將產(chǎn)生一個(gè)數(shù)組,定義為 $a = array( 1=>'one', 3=>'three'); 而不是 $a = array( 1 => 'one', 2 => 'three'); */ $b = array_values($a); // Now b is array(1 => 'one', 2 =>'three') ?> |
foreach 控制結(jié)構(gòu)是專門用于數(shù)組的。它提供了一個(gè)簡單的方法來遍歷數(shù)組。
數(shù)組做什么和不做什么
為什么 $foo[bar] 錯(cuò)了?
應(yīng)該始終在用字符串表示的數(shù)組索引上加上引號。例如用 $foo[‘bar’] 而不是 $foo[bar]。但是為什么 $foo[bar] 錯(cuò)了呢?你可能在老的腳本中見過如下語法
<?php $foo[bar] = 'enemy'; echo $foo[bar]; // etc ?> |
這樣是錯(cuò)的,但可以正常運(yùn)行。那么為什么錯(cuò)了呢?原因是此代碼中有一個(gè)未定義的常量(bar)而不是字符串(’bar’-注意引號),而 PHP 可能會在以后定義此常量,不幸的是你的代碼中有同樣的名字。它能運(yùn)行,是因?yàn)?PHP 自動將裸字符串(沒有引號的字符串且不對應(yīng)于任何已知符號)轉(zhuǎn)換成一個(gè)其值為該裸字符串的正常字符串。例如,如果沒有常量定義為 bar,PHP 將把它替代為 ‘bar’ 并使用之。
注: 這并不意味著總是給鍵名加上引號。用不著給鍵名為常量 或 變量 的加上引號,否則會使 PHP 不能解析它們。
<?php error_reporting(E_ALL); ini_set('display_errors', true); ini_set('html_errors', false); // Simple array: $array = array(1, 2); $count = count($array); for ($i = 0; $i < $count; $i++) { echo "nChecking $i: n"; echo "Bad: " . $array['$i'] . "n"; echo "Good: " . $array[$i] . "n"; echo "Bad: {$array['$i']}n"; echo "Good: {$array[$i]}n"; } ?> |
注: 上面例子輸出為:
Checking 0: Notice: Undefined index: $i in /path/to/script.html on line 9 Bad: Good: 1 Notice: Undefined index: $i in /path/to/script.html on line 11 Bad: Good: 1 Checking 1: Notice: Undefined index: $i in /path/to/script.html on line 9 Bad: Good: 2 Notice: Undefined index: $i in /path/to/script.html on line 11 Bad: Good: 2 |
演示此效應(yīng)的更多例子:
'apple', 'veggie' => 'carrot'); // 正確 print $arr['fruit']; // apple print $arr['veggie']; // carrot // 不正確。This works but also throws a PHP error of // level E_NOTICE because of an undefined constant named fruit // // Notice: Use of undefined constant fruit - assumed 'fruit' in... print $arr[fruit]; // apple // Let's define a constant to demonstrate what's going on. We // will assign value 'veggie' to a constant named fruit. define('fruit','veggie'); // Notice the difference now print $arr['fruit']; // apple print $arr[fruit]; // carrot // The following is okay as it's inside a string. Constants are not // looked for within strings so no E_NOTICE error here print "Hello $arr[fruit]"; // Hello apple // With one exception, braces surrounding arrays within strings // allows constants to be looked for print "Hello {$arr[fruit]}"; // Hello carrot print "Hello {$arr['fruit']}"; // Hello apple // This will not work, results in a parse error such as: // Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING' // This of course applies to using autoglobals in strings as well print "Hello $arr['fruit']"; print "Hello $_GET['foo']"; // Concatenation is another option print "Hello " . $arr['fruit']; // Hello apple ?> </TD> |
當(dāng)打開 error_reporting() 來顯示 E_NOTICE 級別的錯(cuò)誤(例如將其設(shè)為 E_ALL)時(shí)將看到這些錯(cuò)誤。默認(rèn)情況下 error_reporting 被關(guān)閉不顯示這些。
和在語法一節(jié)中規(guī)定的一樣,在方括號(“[”和“]”)之間必須有一個(gè)表達(dá)式。這意味著你可以這樣寫:
<?php echo $arr[somefunc($bar)]; ?> |
這是一個(gè)用函數(shù)返回值作為數(shù)組索引的例子。PHP 也可以用已知常量,你可能之前已經(jīng)見過 E_*。
<?php $error_descriptions[E_ERROR] = "A fatal error has occured"; $error_descriptions[E_WARNING] = "PHP issued a warning"; $error_descriptions[E_NOTICE] = "This is just an informal notice"; ?> |
注意 E_ERROR 也是個(gè)合法的標(biāo)識符,就和第一個(gè)例子中的 bar 一樣。但是上一個(gè)例子實(shí)際上和如下寫法是一樣的:
<?php $error_descriptions[1] = "A fatal error has occured"; $error_descriptions[2] = "PHP issued a warning"; $error_descriptions[8] = "This is just an informal notice"; ?> |
因?yàn)?E_ERROR 等于 1,等等。
如同我們在以上例子中解釋的那樣,$foo[bar] 起作用但其實(shí)是錯(cuò)誤的。它起作用是因?yàn)楦鶕?jù)語法的預(yù)期,bar 被當(dāng)成了一個(gè)常量表達(dá)式。然而,在這個(gè)例子中不存在名為 bar 的常量。PHP 就假定你指的是字面上的 bar,也就是字符串 “bar”,但你忘記寫引號了。
那么為什么這樣做不好?
在未來的某一時(shí)刻,PHP 開發(fā)小組可能會想新增一個(gè)常量或者關(guān)鍵字,或者您可能希望在以后在您的程序中引入新的常量,那你就有麻煩了。例如你已經(jīng)不能這樣用 empty 和 default 這兩個(gè)詞了,因?yàn)樗麄兪潜A糇帧?/p>
注: 重申一次,在雙引號字符串中,不給索引加上引號是合法的因此 “$foo[bar]”是合法的。至于為什么參見以上的例子和字符串中的變量解析中的解釋。
轉(zhuǎn)換為數(shù)組
對于任何的類型:整型、浮點(diǎn)、字符串、布爾和資源,如果您將一個(gè)值轉(zhuǎn)換為數(shù)組,您將得到一個(gè)僅有一個(gè)元素的數(shù)組(其下標(biāo)為 0),該元素即為此標(biāo)量的值。
如果您將一個(gè)對象轉(zhuǎn)換成一個(gè)數(shù)組,您所得到的數(shù)組的元素為該對象的屬性(成員變量),其鍵名為成員變量名。
如果您將一個(gè) NULL 值轉(zhuǎn)換成數(shù)組,您將得到一個(gè)空數(shù)組。
例子
PHP 中的數(shù)組類型有非常多的用途,因此這里有一些例子展示數(shù)組的完整威力。
'red', 'taste' => 'sweet', 'shape' => 'round', 'name' => 'apple', 4 // key will be 0 ); // is completely equivalent with $a['color'] = 'red'; $a['taste'] = 'sweet'; $a['shape'] = 'round'; $a['name'] = 'apple'; $a[] = 4; // key will be 0 $b[] = 'a'; $b[] = 'b'; $b[] = 'c'; // will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'), // or simply array('a', 'b', 'c') ?> |
例子 11-4. 使用 array()
4, 'OS' => 'Linux', 'lang' => 'english', 'short_tags' => true ); // strictly numerical keys $array = array( 7, 8, 0, 156, -10 ); // this is the same as array(0 => 7, 1 => 8, ...) $switching = array( 10, // key = 0 5 => 6, 3 => 7, 'a' => 4, 11, // key = 6 (maximum of integer-indices was 5) '8' => 2, // key = 8 (integer!) '02' => 77, // key = '02' 0 => 12 // the value 10 will be overwritten by 12 ); // empty array $empty = array(); ?> |
例子 11-5. 集合
<?php $colors = array('red', 'blue', 'green', 'yellow'); foreach ($colors as $color) { echo "Do you like $color?n"; } /* output: Do you like red? Do you like blue? Do you like green? Do you like yellow? */ ?> |
注意目前不可能在這樣一個(gè)循環(huán)中直接改變數(shù)組的值??梢愿淖兊睦尤缦拢?br /> 例子 11-6. 集合
$color) { // won't work: //$color = strtoupper($color); //works: $colors[$key] = strtoupper($color); } print_r($colors); /* output: Array ( [0] => RED [1] => BLUE [2] => GREEN [3] => YELLOW ) */ ?> |
本例產(chǎn)生一個(gè)基于一的數(shù)組。 例子 11-7. 基于一的數(shù)組
'January', 'February', 'March'); print_r($firstquarter); /* output: Array ( [1] => 'January' [2] => 'February' [3] => 'March' ) */ ?> |