陣列

PHP 中的陣列實際上是一個有序圖。圖是一種把 values 映射到 keys 的類型。此類型在很多方面做了改善,因此可以把它當成真正的陣列來使用,或清單(向量),散清單(是圖的一種實現),字典,集合,棧,佇列以及更多可能性。因為可以用另一個 PHP 陣列作為值,也可以很容易地模擬樹。

解釋這些結構超出了本手冊的範圍,但對於每種結構至少會發現一個例子。要得到這些結構的更多訊息,建議參考有關此廣闊主旨的外部著作。

語法

定義 array()

可以用 array() 語系結構來新增一個 array。它接受一定數量用逗號分隔的 key => value 參數對。

array( [key =>]
value
     , ...
     )
// key 可以是 integer 或是 string
// value 可以是任何值

<?php
$arr 
= array("foo" => "bar"12 => true);

echo 
$arr["foo"]; // bar
echo $arr[12];    // 1
?>

key 可以是 integer 或是 string。若果鍵名是一個 integer 的標準表達方法,則被解釋為整數(例如 "8" 將被解釋為 8,而 "08" 將被解釋為 "08")。key 中的浮點數被取整為 integer。PHP 中沒有不同的數字下標和關聯下標陣列,陣列的類型只有一種,它可以同時包括整型和字串型的下標。

值可以是任何值。

<?php
$arr 
= array("somearray" => array(=> 513 => 9"a" => 42));

echo 
$arr["somearray"][6];    // 5
echo $arr["somearray"][13];   // 9
echo $arr["somearray"]["a"];  // 42
?>

若果對給出的值沒有指定鍵名,則取現用的最大的整數索引值,而新的鍵名將是該值加一。若果特殊的鍵名已經有了值,則該值會被覆蓋。

<?php
// This array is the same as ...
array(=> 433256"b" => 12);

// ...this array
array(=> 43=> 32=> 56"b" => 12);
?>

警示

自 PHP 4.3.0 起,上述的索引建立方法改變了。如今若果給一個現用的最大鍵名是負值的陣列增加一個新值,則新建立的的索引將為零(0)。以前新建立的索引為現用的最大索引加一,和正值的索引相同。

使用 TRUE 作為鍵名將使 integer 1 成為鍵名。使用 FALSE 作為鍵名將使 integer 0 成為鍵名。使用 NULL 作為鍵名將等同於使用空字串。使用空字串作為鍵名將新增(或覆蓋)一個用空字串作為鍵名的值,這和用空的方括號不一樣。

不能用陣列和物件作為鍵名。這樣做會導致一個警示:Illegal offset type

用方括號的語法新增/修改

可以通過明示地設定值來改變一個現有的陣列。

這是通過在方括號內指定鍵名來給陣列賦值實現的。也可以省略鍵名,在這種情況下給變量名加上一對空的方括號(「[]」)。
$arr[key] = value;
$arr[] = value;
// key 可以是 integer 或是 string
// value 可以為任何值。
若果 $arr 還不存在,將會新增一個。這也是一種定義陣列的置換方法。要改變一個值,只要給它賦一個新值。若果要移除一個鍵名/值對,要對它用 unset()

<?php
$arr 
= array(=> 112 => 2);
$arr[] = 56;    // This is the same as $arr[13] = 56;
                // at this point of the script
$arr["x"] = 42// This adds a new element to
                // the array with key "x"
unset($arr[5]); // This removes the element from the array
unset($arr);    // This deletes the whole array
?>

注: 如上所述,若果給出方括號但沒有指定鍵名,則取現用的最大整數索引值,新的鍵名將是該值 + 1。若果現用的還沒有整數索引,則鍵名將為 0。若果特殊的鍵名已經有值了,該值將被覆蓋。

警示

自 PHP 4.3.0 起,上述的索引建立方法改變了。如今若果給一個現用的最大鍵名是負值的陣列增加一個新值,則新建立的的索引將為零(0)。以前新建立的索引為現用的最大索引加一,和正值的索引相同。

注意這裡所使用的最大整數鍵名不一定現用的就在陣列中。它只要在上次陣列重新建立索引後曾經存在過就行了。以下面的例子來說明:

<?php
// 建立一個簡單的陣列
$array = array(12345);
print_r($array);

// 現在移除其中的所有單元,但保持陣列本身的結構
foreach ($array as $i => $value) {
    unset(
$array[$i]);
}
print_r($array);

// 增加一個單元(注意新的鍵名是 5,而不是你可能以為的 0)
$array[] = 6;
print_r($array);

// 重新索引:
$array array_values($array);
$array[] = 7;
print_r($array);
?>

上例將輸出:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Array
(
)
Array
(
    [5] => 6
)
Array
(
    [0] => 6
    [1] => 7
)

實用函數

有相當多的實用函數作用於陣列,參見陣列函數一節。

注: unset() 函數容許取消一個陣列中的鍵名。要注意陣列將不會重建索引。

<?PHP
$a 
= array( => 'one'=> 'two'=> 'three' );
unset( 
$a[2] );
/* 將產生一個陣列,定義為
   $a = array( 1=>'one', 3=>'three');
   而不是
   $a = array( 1 => 'one', 2 => 'three');
*/
$b array_values($a);
// Now $b is array(0 => 'one', 1 =>'three')
?>

foreach 控制結構是專門用於陣列的。它提供了一個簡單的方法來遍歷陣列。

陣列做什麼和不做什麼

為什麼 $foo[bar] 錯了?

應該始終在用字串表示的陣列索引上加上引號。例如用 $foo['bar'] 而不是 $foo[bar]。但是為什麼 $foo[bar] 錯了呢?可能在老的腳本中見過如下語法:

<?php
$foo
[bar] = 'enemy';
echo 
$foo[bar];
// etc
?>

這樣是錯的,但可以標準運行。那麼為什麼錯了呢?原因是此代碼中有一個未定義的常量(bar)而不是字串('bar'-注意引號),而 PHP 可能會在以後定義此常量,不幸的是你的代碼中有同樣的名字。它能運行,是因為 PHP 自動將裸字串(沒有引號的字串且不對應於任何已知符號)轉換成一個其值為該裸字串的標準字串。例如,若果沒有常量定義為 bar,PHP 將把它替代為 'bar' 並使用之。

注: 這並不意味著總是給鍵名加上引號。用不著給鍵名為常量變量的加上引號,否則會使 PHP 不能解析它們。

<?php
error_reporting
(E_ALL);
ini_set('display_errors'true);
ini_set('html_errors'false);
// Simple array:
$array = array(12);
$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

演示此效應的更多例子:

<?php
// 顯示所有錯誤
error_reporting(E_ALL);

$arr = array('fruit' => '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
?>

當開啟 error_reporting() 來顯示 E_NOTICE 層級的錯誤(例如將其設為 E_ALL)時將看到這些錯誤。預設情況下 error_reporting 被關閉不顯示這些。

和在語法一節中規定的一樣,在方括號(「[」和「]」)之間必須有一個表達式。這意味著可以這樣寫:

<?php
echo $arr[somefunc($bar)];
?>

這是一個用函數返回值作為陣列索引的例子。PHP 也可以用已知常量,可能之前已經見過 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 也是個合法的標識符,就和第一個例子中的 bar 一樣。但是上一個例子實際上和如下寫法是一樣的:

<?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";
?>

因為 E_ERROR 等於 1,等等。

如同在以上例子中解釋的那樣,$foo[bar] 起作用但其實是錯誤的。它起作用是因為根據語法的預期,bar 被當成了一個常量表達式。然而,在這個例子中不存在名為 bar 的常量。PHP 就假定指的是字面上的 bar,也就是字串 "bar",但忘記加引號了。

那麼為什麼這樣做不好?

在未來的某一時刻,PHP 開發小組可能會想新增一個常量或是關鍵字,或是會員可能希望以後在自己的程式中引入新的常量,那就有麻煩了。例如已經不能這樣用 emptydefault 這兩個詞了,因為他們是保留字

注: 重申一次,在雙引號字串中,不給索引加上引號是合法的因此 "$foo[bar]"是合法的。至於為什麼參見以上的例子和字串中的變量解析中的解釋。

轉換為陣列

對於任何的類型:整型、浮點、字串、布爾和資源,若果將一個值轉換為陣列,將得到一個僅有一個元素的陣列(其下標為 0),該元素即為此標量的值。

若果將一個物件轉換成一個陣列,所得到的陣列的元素為該對象的屬性(成員變量),其鍵名為成員變量名。

若果將一個 NULL 值轉換成陣列,將得到一個空陣列。

比較

有可能通過 array_diff()陣列運算符來比較陣列。

例子

PHP 中的陣列類型有非常多的用途,因此這裡有一些例子展示陣列的完整威力。

<?php
// this
$a = array( 'color' => '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-6. 使用 array()

<?php
// Array as (property-)map
$map = array( 'version'    => 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-7. 集合

<?php
$colors 
= array('red''blue''green''yellow');

foreach (
$colors as $color) {
    echo 
"Do you like $color?\n";
}

?>

上例將輸出:

Do you like red?
Do you like blue?
Do you like green?
Do you like yellow?

直接改變陣列的值在 PHP 5 中可以通過引用傳遞來做到。之前的版本需要需要採取別的方法:

例子 11-8. 集合

<?php
// PHP 5
foreach ($colors as &$color) {
    
$color strtoupper($color);
}
unset(
$color); /* 確保下面對 $color 的覆蓋不會影響到前一個陣列單元 */
// 之前版本的方法
foreach ($colors as $key => $color) {
    
$colors[$key] = strtoupper($color);
}

print_r($colors);
?>

上例將輸出:

Array
(
    [0] => RED
    [1] => BLUE
    [2] => GREEN
    [3] => YELLOW
)

本例產生一個下標從 1 開始的陣列。

例子 11-9. 下標從 1 開始的陣列

<?php
$firstquarter  
= array(=> 'January''February''March');
print_r($firstquarter);
?>

上例將輸出:

Array
(
    [1] => 'January'
    [2] => 'February'
    [3] => 'March'
)
*/
?>

例子 11-10. 填充陣列

<?php
// fill an array with all items from a directory
$handle opendir('.');
while (
false !== ($file readdir($handle))) {
    
$files[] = $file;
}
closedir($handle);
?>

陣列是有序的。也可以使用不同的排序函數來改變順序。更多訊息參見陣列函數。可以用 count() 函數來數出陣列中元素的個數。

例子 11-11. 陣列排序

<?php
sort
($files);
print_r($files);
?>

因為陣列中的值可以為任意值,也可是另一個陣列。這樣可以產生遞歸或多維陣列。

例子 11-12. 遞歸和多維陣列

<?php
$fruits 
= array ( "fruits"  => array ( "a" => "orange",
                                       
"b" => "banana",
                                       
"c" => "apple"
                                     
),
                  
"numbers" => array ( 1,
                                       
2,
                                       
3,
                                       
4,
                                       
5,
                                       
6
                                     
),
                  
"holes"   => array (      "first",
                                       
=> "second",
                                            
"third"
                                     
)
                );

// Some examples to address values in the array above
echo $fruits["holes"][5];    // prints "second"
echo $fruits["fruits"]["a"]; // prints "orange"
unset($fruits["holes"][0]);  // remove "first"

// Create a new multi-dimensional array
$juices["apple"]["green"] = "good";
?>

需要注意陣列的賦值總是會涉及到值的複製。需要在複製陣列時用引用符號(&)。

<?php
$arr1 
= array(23);
$arr2 $arr1;
$arr2[] = 4// $arr2 is changed,
             // $arr1 is still array(2,3)

$arr3 = &$arr1;
$arr3[] = 4// now $arr1 and $arr3 are the same
?>