手册
H1 class=sect1>
在PHP中一个数组实际上是一个有次序的映射。一个映射是映射值到关键字上。这个类型在单独的方法上被优化的,你可以作为一个真实的数组或一个列表(向量),hashtable (一个映射的执行),字典,聚集,堆栈,队列和更多的来使用它。因为你可能还有另外的PHP-数组作为一个值,你也可以十分容易的模仿树结构。
这个结构的解释超过了这本手册的范围,但是你将发现为这结构的最小的范例。关于这个结构的更多信息,请你查阅其它文献。
array()指定数组
一个数组可以被array() 构造。它由一对key => value并用逗号分割的一系列的号码组成。
一个 key 是任意的非负整数或一个字符串组成。 如果一个是由一个标准的非负整数表达的,它将被解释成这样(i.e. '8' 将被解释成8,'08' 将被解释成'08').
一个值可以是任意的。
忽略键。如果你忽略一个键,那么新键将用最大的整数索引加一。如果整数索引也不存在,这个键将是0。如果你已经指定一个值给一个键,那么这个将被复盖。
array( [key =>] value
, ...
)
// 键是任意的字符串或非负整数
// 值可以是任意的
你可以通过明确的设置值去修改一个已存在的数组。
可以用带方括号的键去分配值给数组。你也可以忽略这个键,在变量名后加一对空方括号。 $arr[key] = value;
$arr[] = value;
// key 是任意字符串或非负整数
// value 可以是任意的
如果$arr 不存在,它将被新建。如此也可能选择性的去指定一个数组。去改变一个确定的值,刚好分配一个新值给它。如果你想去删除一对键/值,你需要用 unset() 。
为数组的工作,有一睦有用的函数,参见数组函数 段落。
foreach 流程控制明确提供了一个容易的方法去循环一个数组。
在旧的脚本中你可能看到过下边的语法:
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
这是错误的,但它会工作。然而,为什么是错误的呢?在这之后的syntax 片段中规定,表达式必须在方括号之间。。这意味着你可以象下边一样做:
echo $arr[ foo(true) ];
这个例子使用一个函数的返回值作为数组的索引。PHP也知道是常量,你可以见E_*。
$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 。But the last example is in fact the same as writing:
$error_descriptions[1] = "A fatal error has occured";
$error_descriptions[2] = "PHP issued a warning";
$error_descriptions[8] = "This is just an informal notice";
because E_ERROR equals 1, etc.
Then, how is it possible that $foo[bar] works? It works, because bar is due to its syntax expected to be a constant expression. However, in this case no constant with the name bar exists. PHP now assumes that you meant bar literally, as the string "bar", but that you forgot to write the quotes.
At some point in the future, the PHP team might want to add another constant or keyword, and then you get in trouble. For example, you already cannot use the words empty and default this way, since they are special keywords.
And, if these arguments don't help: this syntax is simply deprecated, and it might stop working some day.
Tip: When you turn error_reporting to E_ALL, you will see that PHP generates warnings whenever this construct is used. This is also valid for other deprecated 'features'. (put the line error_reporting(E_ALL); in your script)
Note: Inside a double-quoted string, an other syntax is valid. See variable parsing in strings for more details.
The array type in PHP is very versatile, so here will be some examples to show you the full power of arrays.
// 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')
Example 6-4. Using array()
// 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();
Example 6-5. Collection
$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?
*/
Note that it is currently not possible to change the values of the array directly in such a loop. A workaround is the following:
Example 6-6. Collection
foreach ( $colors as $key => $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
)
*/
This example creates a one-based array.
Example 6-7. One-based index
$firstquarter = array(1 => 'January', 'February', 'March');
print_r($firstquarter);
/* output:
Array
(
[1] => 'January'
[2] => 'February'
[3] => 'March'
)
*/
Example 6-8. Filling real array
// fill an array with all items from a directory
$handle = opendir('.');
while ($file = readdir($handle))
{
$files[] = $file;
}
closedir($handle);
Arrays are ordered. You can also change the order using various sorting-functions. See array-functions for more information.
Example 6-9. Sorting array
sort($files);
print_r($files);
Because the value of an array can be everything, it can also be another array. This way you can make recursive and multi-dimensional arrays.
Example 6-10. Recursive and multi-dimensional arrays
$fruits = array ( "fruits" => array ( "a" => "orange"
, "b" => "banana"
, "c" => "apple"
)
, "numbers" => array ( 1
, 2
, 3
, 4
, 5
, 6
)
, "holes" => array ( "first"
, 5 => "second"
, "third"
)
);

PHPSession失效的原因包括配置錯誤、Cookie問題和Session過期。 1.配置錯誤:檢查並設置正確的session.save_path。 2.Cookie問題:確保Cookie設置正確。 3.Session過期:調整session.gc_maxlifetime值以延長會話時間。

在PHP中調試會話問題的方法包括:1.檢查會話是否正確啟動;2.驗證會話ID的傳遞;3.檢查會話數據的存儲和讀取;4.查看服務器配置。通過輸出會話ID和數據、查看會話文件內容等方法,可以有效診斷和解決會話相關的問題。

多次調用session_start()會導致警告信息和可能的數據覆蓋。 1)PHP會發出警告,提示session已啟動。 2)可能導致session數據意外覆蓋。 3)使用session_status()檢查session狀態,避免重複調用。

在PHP中配置會話生命週期可以通過設置session.gc_maxlifetime和session.cookie_lifetime來實現。 1)session.gc_maxlifetime控制服務器端會話數據的存活時間,2)session.cookie_lifetime控制客戶端cookie的生命週期,設置為0時cookie在瀏覽器關閉時過期。

使用數據庫存儲會話的主要優勢包括持久性、可擴展性和安全性。 1.持久性:即使服務器重啟,會話數據也能保持不變。 2.可擴展性:適用於分佈式系統,確保會話數據在多服務器間同步。 3.安全性:數據庫提供加密存儲,保護敏感信息。

在PHP中實現自定義會話處理可以通過實現SessionHandlerInterface接口來完成。具體步驟包括:1)創建實現SessionHandlerInterface的類,如CustomSessionHandler;2)重寫接口中的方法(如open,close,read,write,destroy,gc)來定義會話數據的生命週期和存儲方式;3)在PHP腳本中註冊自定義會話處理器並啟動會話。這樣可以將數據存儲在MySQL、Redis等介質中,提升性能、安全性和可擴展性。

SessionID是網絡應用程序中用來跟踪用戶會話狀態的機制。 1.它是一個隨機生成的字符串,用於在用戶與服務器之間的多次交互中保持用戶的身份信息。 2.服務器生成並通過cookie或URL參數發送給客戶端,幫助在用戶的多次請求中識別和關聯這些請求。 3.生成通常使用隨機算法保證唯一性和不可預測性。 4.在實際開發中,可以使用內存數據庫如Redis來存儲session數據,提升性能和安全性。

在無狀態環境如API中管理會話可以通過使用JWT或cookies來實現。 1.JWT適合無狀態和可擴展性,但大數據時體積大。 2.Cookies更傳統且易實現,但需謹慎配置以確保安全性。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Dreamweaver CS6
視覺化網頁開發工具