搜尋
首頁後端開發php教程php數組(Array)

php數組(Array)

Nov 24, 2016 pm 02:00 PM
phpphp數組

PHP 中的陣列其實是一個有序映射。映射是一種把 values 關聯到 keys 的型別。此類型在很多方面做了優化,因此可以把它當成真正的數組,或列表(向量),散列表(是映射的一種實現),字典,集合,棧,隊列以及更多可能性。由於數組元素的值也可以是另一個數組,樹狀結構和多維數組也是允許的。

解釋這些結構超出了本手冊的範圍,但對於每種結構至少會提供一個例子。要得到這些結構的更多信息,建議參考有關此廣闊主題的其它著作。

語法

定義陣列 array()

可以用 array() 語言結構來新建一個陣列。它接受任意數量用逗號分隔的 鍵(key) => 值(value)對。

array( key => value
, ...
)
// 鍵(key)可是一個整數integer 或字串string
// 值(value)可以是任意型別的值

最後一個陣列單元之後的逗號可以省略。通常用於單行數組定義中,例如常用 array(1, 2) 而不是 array(1, 2, )。對多行數組定義通常保留最後一個逗號,這樣要新增一個單元時會更方便。

自 5.4 起可以使用短數組定義語法,並以 [] 取代 array()。

Example #1 一個簡單陣列

<?php
$array = array(
   "foo" => "bar",
   "bar" => "foo",
);
 
// 自 PHP 5.4 起
$array = [
   "foo" => "bar",
   "bar" => "foo",
];
?>

key 可以是 integer 或 string。 value 可以是任意型別。

此外 key 會有如下的強制轉換:

包含有合法整數值的字串會被轉換為整數。例如鍵名 "8" 實際上會儲存為 8。但是 "08" 則不會強制轉換,因為其不是一個合法的十進制數值。

浮點數也會被轉換為整數,表示其小數部分會被捨去。例如鍵名 8.7 實際上會被儲存為 8。

布爾值也會被轉換成整數型。即鍵名 true 實際上會儲存為 1 而鍵名 false 會被儲存為 0。

Null 會被轉換為空字串,即鍵名 null 實際會儲存為 ""。

數組和物件不能被用為鍵名。堅持這麼做會導致警告:Illegal offset type。

如果在陣列定義中多個單元都使用了同一個鍵名,則只使用了最後一個,之前的都被覆蓋了。

Example #2 類型強制與覆蓋範例

<?php
$array = array(
   1    => "a",
   "1"  => "b",
   1.5  => "c",
   true => "d",
);
var_dump($array);
?>

以上例程會輸出:

array(1) {
[1]=>
string(1) "d"
}

上例中所有的鍵名都被強制轉換為1,則每一個新單元都會覆蓋前一個的值,最後剩下的只有一個"d"。

PHP 數組可以同時含有 integer 和 string 類型的鍵名,因為 PHP 實際上並沒有區分索引數組和關聯數組。

如果對給出的值沒有指定鍵名,則取目前最大的整數索引值,而新的鍵名將是該值加一。如果指定的鍵名已經有了值,則該值會被覆寫。

Example #3 混合 integer 和 string 鍵名

<?php
$array = array(
   "foo" => "bar",
   "bar" => "foo",
   100   => -100,
   -100  => 100,
);
var_dump($array);
?>

以上程式會輸出:

array(4) {
["foo"]=>
string(3) "bar"
["bar"]=>
string(3) "foo"
[100]=>
int(-100)
[-100]=>
int(100)
}

key 為可選項。如果未指定,PHP 將自動使用先前用過的最大 integer 鍵名加上 1 作為新的鍵名。

Example #4 沒有鍵名的索引數組

<?php
$array = array("foo", "bar", "hallo", "world");
var_dump($array);
?>

以上例程會輸出:

array(4) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[2]=>
string(5) "hallo"
[3]=>
string(5) "world"
}

還可以只對某些單元指定鍵名而對其它的空置:

Example #5 僅對部分單元指定鍵名名

<?php
$array = array(
        "a",
        "b",
   6 => "c",
        "d",
);
var_dump($array);
?>

以上例程會輸出:

array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[6]=>
string(1) "c"
[7]=>
string(1) "d"
}

可以看到最後一個值"d" 自動賦予了鍵名7。這是由於之前最大的整數鍵名稱是 6。

用方括號語法存取數組單元

數組單元可以透過 array[key] 語法存取。

Example #6 存取陣列單元

<?php
$array = array(
   "foo" => "bar",
   42    => 24,
   "multi" => array(
        "dimensional" => array(
            "array" => "foo"
        )
   )
);
 
var_dump($array["foo"]);
var_dump($array[42]);
var_dump($array["multi"]["dimensional"]["array"]);
?>

以上程式會輸出:

string(3) "bar"
int(24)
string(3) "foo"

Note:

方括號和花括號可以互換使用來存取陣列單元(例如$array[42] 和$array}在上例中效果相同)。

自 PHP 5.4 起可以用陣列間接引用函數或方法呼叫的結果。之前只能通過一個臨時變數。

自 PHP 5.5 起可以用陣列間接引用一個陣列原型。

Example #7 陣列間接引用

<?php
function getArray() {
   return array(1, 2, 3);
}
 
// on PHP 5.4
$secondElement = getArray()[1];
 
// previously
$tmp = getArray();
$secondElement = $tmp[1];
// or
list(, $secondElement) = getArray();
?>

Note:

試圖存取一個未定義的陣列鍵名與存取任何未定義變數一樣:會導致 E_NOTICE 層級錯誤訊息,其結果為 NULL。

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

可以透過明示地設定其中的值來修改一個已有數組。

這是透過在方括號內指定鍵名來賦值陣列來實現的。也可以省略鍵名,在這種情況下為變數名稱加上一對空的方括號([])。

$arr[key] = value;
$arr[] = value;
// key 可以是 integer 或 string
// value 可以是任意类型的值

如果 $arr 还不存在,将会新建一个,这也是另一种新建数组的方法。不过并不鼓励这样做,因为如果 $arr 已经包含有值(例如来自请求变量的 string)则此值会保留而 [] 实际上代表着字符串访问运算符。初始化变量的最好方式是直接给其赋值。。

要修改某个值,通过其键名给该单元赋一个新值。要删除某键值对,对其调用 unset() 函数。

<?php
$arr = array(5 => 1, 12 => 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
?>

Note:

如上所述,如果给出方括号但没有指定键名,则取当前最大整数索引值,新的键名将是该值加上 1(但是最小为 0)。如果当前还没有整数索引,则键名将为 0。

注意这里所使用的最大整数键名不一定当前就在数组中。它只要在上次数组重新生成索引后曾经存在过就行了。以下面的例子来说明:

<?php
// 创建一个简单的数组
$array = array(1, 2, 3, 4, 5);
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
)

实用函数

有很多操作数组的函数,参见数组函数一节。

Note:

unset() 函数允许删除数组中的某个键。但要注意数组将不会重建索引。如果需要删除后重建索引,可以用 array_values() 函数。

?php
$a = array(1 => &#39;one&#39;, 2 => &#39;two&#39;, 3 => &#39;three&#39;);
unset($a[2]);
/* will produce an array that would have been defined as
  $a = array(1 => &#39;one&#39;, 3 => &#39;three&#39;);
  and NOT
  $a = array(1 => &#39;one&#39;, 2 =>&#39;three&#39;);
*/
 
$b = array_values($a);
// Now $b is array(0 => &#39;one&#39;, 1 =>&#39;three&#39;)
?>

foreach 控制结构是专门用于数组的。它提供了一个简单的方法来遍历数组。

 

数组做什么和不做什么

为什么 $foo[bar] 错了?

应该始终在用字符串表示的数组索引上加上引号。例如用 $foo['bar'] 而不是 $foo[bar]。但是为什么呢?可能在老的脚本中见过如下语法:

<?php
$foo[bar] = &#39;enemy&#39;;
echo $foo[bar];
// etc
?>

这样是错的,但可以正常运行。那么为什么错了呢?原因是此代码中有一个未定义的常量(bar)而不是字符串('bar'-注意引号),而 PHP 可能会在以后定义此常量,不幸的是你的代码中有同样的名字。它能运行,是因为 PHP 自动将裸字符串(没有引号的字符串且不对应于任何已知符号)转换成一个其值为该裸字符串的正常字符串。例如,如果没有常量定义为 bar,PHP 将把它替代为 'bar' 并使用之。

Note: 这并不意味着总是给键名加上引号。用不着给键名为常量或变量的加上引号,否则会使 PHP 不能解析它们。

<?php
error_reporting(E_ALL);
ini_set(&#39;display_errors&#39;, true);
ini_set(&#39;html_errors&#39;, false);
// Simple array:
$array = array(1, 2);
$count = count($array);
for ($i = 0; $i < $count; $i++) {
   echo "\nChecking $i: \n";
   echo "Bad: " . $array[&#39;$i&#39;] . "\n";
   echo "Good: " . $array[$i] . "\n";
   echo "Bad: {$array[&#39;$i&#39;]}\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
// Show all errors
error_reporting(E_ALL);
 
$arr = array(&#39;fruit&#39; => &#39;apple&#39;, &#39;veggie&#39; => &#39;carrot&#39;);
 
// Correct
print $arr[&#39;fruit&#39;];  // apple
print $arr[&#39;veggie&#39;]; // carrot
 
// Incorrect.  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 &#39;fruit&#39; in...
print $arr[fruit];    // apple
 
// This defines a constant to demonstrate what&#39;s going on.  The value &#39;veggie&#39;
// is assigned to a constant named fruit.
define(&#39;fruit&#39;, &#39;veggie&#39;);
 
// Notice the difference now
print $arr[&#39;fruit&#39;];  // apple
print $arr[fruit];    // carrot
 
// The following is okay, as it&#39;s inside a string. Constants are not looked for
// within strings, so no E_NOTICE occurs here
print "Hello $arr[fruit]";      // Hello apple
 
// With one exception: braces surrounding arrays within strings allows constants
// to be interpreted
print "Hello {$arr[fruit]}";    // Hello carrot
print "Hello {$arr[&#39;fruit&#39;]}";  // Hello apple
 
// This will not work, and will result in a parse error, such as:
// Parse error: parse error, expecting T_STRING&#39; or T_VARIABLE&#39; or T_NUM_STRING&#39;
// This of course applies to using superglobals in strings as well
print "Hello $arr[&#39;fruit&#39;]";
print "Hello $_GET[&#39;foo&#39;]";
 
// Concatenation is another option
print "Hello " . $arr[&#39;fruit&#39;]; // Hello apple
?>

当打开 error_reporting 来显示 E_NOTICE 级别的错误(将其设为 E_ALL)时将看到这些错误。默认情况下 error_reporting 被关闭不显示这些。

和在语法一节中规定的一样,在方括号(“[”和“]”)之间必须有一个表达式。这意味着可以这样写:

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

这是一个用函数返回值作为数组索引的例子。PHP 也可以用已知常量,可能之前已经见过:

<?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,等等。

那么为什么这样做不好?

也许有一天,PHP 开发小组可能会想新增一个常量或者关键字,或者用户可能希望以后在自己的程序中引入新的常量,那就有麻烦了。例如已经不能这样用 empty 和 default 这两个词了,因为他们是保留字。

Note: 重申一次,在双引号字符串中,不给索引加上引号是合法的因此 "$foo[bar]" 是合法的(“合法”的原文为 valid。在实际测试中,这么做确实可以访问数组的该元素,但是会报一个常量未定义的 notice。无论如何,强烈建议不要使用 $foo[bar]这样的写法,而要使用 $foo['bar'] 来访问数组中元素。--haohappy 注)。至于为什么参见以上的例子和字符串中的变量解析中的解释。

转换为数组

对于任意 integer,float,string,boolean 和 resource 类型,如果将一个值转换为数组,将得到一个仅有一个元素的数组,其下标为 0,该元素即为此标量的值。换句话说,(array)$scalarValue 与 array($scalarValue) 完全一样。

如果一个 object 类型转换为 array,则结果为一个数组,其单元为该对象的属性。键名将为成员变量名,不过有几点例外:整数属性不可访问;私有变量前会加上类名作前缀;保护变量前会加上一个 '*' 做前缀。这些前缀的前后都各有一个 NULL 字符。这会导致一些不可预知的行为:

<?php
 
class A {
   private $A; // This will become &#39;\0A\0A&#39;
}
 
class B extends A {
   private $A; // This will become &#39;\0B\0A&#39;
   public $AA; // This will become &#39;AA&#39;
}
 
var_dump((array) new B());
?>

上例会有两个键名为 'AA',不过其中一个实际上是 '\0A\0A'。

将 NULL 转换为 array 会得到一个空的数组。

比较

可以用 array_diff() 和数组运算符来比较数组。

示例

PHP 中的数组类型有非常多的用途。以下是一些示例:

<?php
// This:
$a = array( &#39;color&#39; => &#39;red&#39;,
           &#39;taste&#39; => &#39;sweet&#39;,
           &#39;shape&#39; => &#39;round&#39;,
           &#39;name&#39;  => &#39;apple&#39;,
           4        // key will be 0
         );
 
$b = array(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;);
 
// . . .is completely equivalent with this:
$a = array();
$a[&#39;color&#39;] = &#39;red&#39;;
$a[&#39;taste&#39;] = &#39;sweet&#39;;
$a[&#39;shape&#39;] = &#39;round&#39;;
$a[&#39;name&#39;]  = &#39;apple&#39;;
$a[]        = 4;        // key will be 0
 
$b = array();
$b[] = &#39;a&#39;;
$b[] = &#39;b&#39;;
$b[] = &#39;c&#39;;
 
// After the above code is executed, $a will be the array
// array(&#39;color&#39; => &#39;red&#39;, &#39;taste&#39; => &#39;sweet&#39;, &#39;shape&#39; => &#39;round&#39;,
// &#39;name&#39; => &#39;apple&#39;, 0 => 4), and $b will be the array
// array(0 => &#39;a&#39;, 1 => &#39;b&#39;, 2 => &#39;c&#39;), or simply array(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;).
?>

Example #8 使用 array()

<?php
// Array as (property-)map
$map = array( &#39;version&#39;    => 4,
             &#39;OS&#39;         => &#39;Linux&#39;,
             &#39;lang&#39;       => &#39;english&#39;,
             &#39;short_tags&#39; => 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,
                   &#39;a&#39;  =>  4,
                           11, // key = 6 (maximum of integer-indices was 5)
                   &#39;8&#39;  =>  2, // key = 8 (integer!)
                   &#39;02&#39; => 77, // key = &#39;02&#39;
                   0    => 12  // the value 10 will be overwritten by 12
                 );
// empty array
$empty = array();
?>

Example #9 集合

<?php
$colors = array(&#39;red&#39;, &#39;blue&#39;, &#39;green&#39;, &#39;yellow&#39;);
 
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 起可以通过引用传递来做到。之前的版本需要需要采取变通的方法:

Example #10 在循环中改变单元

<?php
// PHP 5
foreach ($colors as &$color) {
   $color = strtoupper($color);
}
unset($color); /* ensure that following writes to
$color will not modify the last array element */
 
// Workaround for older versions
foreach ($colors as $key => $color) {
   $colors[$key] = strtoupper($color);
}
 
print_r($colors);
?>

以上例程会输出:

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

本例生成一个下标从 1 开始的数组。

Example #11 下标从 1 开始的数组

<?php
$firstquarter  = array(1 => &#39;January&#39;, &#39;February&#39;, &#39;March&#39;);
print_r($firstquarter);
?>

以上例程会输出:

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

Example #12 填充数组

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

数组是有序的。也可以使用不同的排序函数来改变顺序。更多信息参见数组函数。可以用 count() 函数来数出数组中元素的个数。

Example #13 数组排序

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

因为数组中的值可以为任意值,也可是另一个数组。这样可以产生递归或多维数组。

Example #14 递归和多维数组

<?php
$fruits = array ( "fruits"  => array ( "a" => "orange",
                                      "b" => "banana",
                                      "c" => "apple"
                                    ),
                 "numbers" => array ( 1,
                                      2,
                                      3,
                                      4,
                                      5,
                                      6
                                    ),
                 "holes"   => array (      "first",
                                      5 => "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";
?>

数组(Array) 的赋值总是会涉及到值的拷贝。使用引用运算符通过引用来拷贝数组。

<?php
$arr1 = array(2, 3);
$arr2 = $arr1;
$arr2[] = 4; // $arr2 is changed,
            // $arr1 is still array(2, 3)
$arr3 = &$arr1;
$arr3[] = 4; // now $arr1 and $arr3 are the same
?>


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
高流量網站的PHP性能調整高流量網站的PHP性能調整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依賴注入:初學者的代碼示例PHP中的依賴注入:初學者的代碼示例May 14, 2025 am 12:08 AM

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

PHP性能:是否可以優化應用程序?PHP性能:是否可以優化應用程序?May 14, 2025 am 12:04 AM

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

PHP性能優化:最終指南PHP性能優化:最終指南May 14, 2025 am 12:02 AM

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

PHP依賴注入容器:快速啟動PHP依賴注入容器:快速啟動May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依賴注入與服務定位器PHP中的依賴注入與服務定位器May 13, 2025 am 12:10 AM

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

PHP性能優化策略。PHP性能優化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP電子郵件驗證:確保正確發送電子郵件PHP電子郵件驗證:確保正確發送電子郵件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!