多維數組沒什麼特別的,只不過是另一個數組中的一個數組。數組的每個索引保存另一個數組而不是單個元素,該元素又可以指向另一個數組或特定元素。使用從外部數組開始並向內部數組移動的多個維度來存取數組內部的這些子數組。維度基本上是存取或儲存數組中特定位置的值所需的索引。 PHP 中的多維數組在即時應用程式中被廣泛使用,但與一維數組相比,處理它們是相當棘手的,因為它們有多個括號,並且使用它們時有些複雜,無論是訪問還是存儲值特定索引,需要使用循環。
廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
文法
下面給的是 PHP 中多維數組的一般語法。 PHP 中的多維數組可以是 2D、3D、4D 等。數組維數越多,管理就越困難,數組名稱前面加的括號就越多。
二維陣列的語法:
array( array(element1, element2, elements3, ...), array(element1, element2, elements3, ...), … so on )
3D 陣列的語法:
array( array ( array(element1, element2, elements3, ...), array(element1, element2, elements3, ...), … so on ), array ( array(element1, element2, elements3, ...), array(element1, element2, elements3, ...), … so on ), … so on )
如何在 PHP 中宣告多維數組?
PHP 允許其多維數組進行索引或關聯。與索引數組相比,關聯數組更具交互性。 PHP 允許使用關鍵字「array」以非常簡單的方式在 PHP 中聲明多維數組。為了在另一個數組中聲明數組,我們需要添加關鍵字“array”,然後添加該數組的元素。
1. PHP 中二維陣列的宣告
代碼:
<?php $employee_details = array(); $employee_details[ ] = array("Ram", "Agra", "Sr. Engineer"); $employee_details[ ] = array("Raghav", "Delhi", "Jr. Engineer"); ?>
或
<?php $employee_details = array( array("Ram", "Agra", "Sr. Engineer"), array("Raghav", "Delhi", "Jr. Engineer"), ); ?>
上面顯示的第二種方法是常用的,因為它很容易理解。
2. PHP 中 3D 陣列的宣告
代碼:
<?php /* Simplest way to declare a 3D array in Php in an indexed manner */ $item_details = array( array( array ("item1", "abc", 100)), array ("item2", "bcd", 200)), array ("item3", "def", 300)), ), array( array ("item4", "abc4", 100)), array ("item5", "bcd5", 200)), array ("item6", "def6", 300)), ), ); ?>
上面的聲明是純粹索引的 3D 陣列之一,因為沒有用於關聯的鍵值對。
如何在 PHP 中初始化多維數組?
初始化多維數組意味著在數組的特定位置或索引處分配值或元素。在 PHP 中初始化多維數組就像聲明一樣非常簡單。唯一要記住的是初始化子數組時使用大括號。在初始化多維數組中的值時,主數組可以是索引數組或關聯數組,在下面給出的範例中,主數組是具有鍵的關聯數組,如 Levis、Lee、Denizen 等,
1.在 PHP 中初始化二維數組
代碼:
<?php /* It is a multidimensional 2D array of clothes in which the main array holds another arrays of having 2 elements like cloth type and quantity */ /* It is associative kind of array having the data in the form of key => value pairs. So the data at the inner subarray is represented as associated by the key element.*/ $clothes = array( "Levis" => array( "Cloth_type" => "jeans", "Quantity" => 20 ), "Pepe" => array( "Cloth_type" => "jeans", "Quantity" => 100 ), "Lee" => array( "Cloth_type" => "tshirts", "Quantity" => 50 ), "Denizen" => array( "Cloth_type" => "tops", "Quantity" => 80 ) ); ?>
2.在 PHP 中初始化 3D 陣列
3D 陣列的初始化與 2D 陣列相同,兩者之間唯一的差異是維度。 3D 陣列比 2D 陣列需要多 1 個索引來初始化。數組維數增加,初始化數組的索引數也會增加。在下面的範例中,主數組是一個簡單的索引數組,其本身俱有子數組。我們還可以將下面範例中的主數組設定為關聯數組,就像我們在二維數組中所做的那樣,將鍵作為品牌名稱,這使得客戶在訪問和儲存它時更容易理解。
代碼:
<?php /* In this there is a 3D array of clothes in which each element have an array of cloth type, brand and quantity of that particular brand. Each brand has different quantity and cloth type.*/ $clothes = array( array( array( "Brand" => "Levis", "Cloth_type" => "jeans", "Quantity" => 20 ), array( "Brand" => "Levis", "Cloth_type" => "Tops", "Quantity" => 100 ) ), array( array( "Brand" => "Lee", "Cloth_type" => "jeans", "Quantity" => 50 ), array( "Brand" => "Lee", "Cloth_type" => "tops", "Quantity" => 80 ) ), ); ?>
在 PHP 中存取多維數組
在 PHP 中存取多維數組非常簡單,透過使用 PHP 中常用的 for 或 foreach 迴圈來完成。對於索引數組,通常可以使用行號和列號來存取數組元素,類似於其他語言,如 C、Java 等(arr[row_Num][column_Num])。
對於關聯數組,多維數組元素的存取是使用鍵和值對(key => Value)來完成的。儘管元素是透過簡單的 for 或 for every 迴圈存取的。請參考下面給出的範例,以清楚了解多維數組中元素的存取。
類型
PHP 中不存在多維數組可以存在的特定狀態。這取決於具體情況和場景。數組的維數相應地變化。通常程式設計師會使用 2D 和 3D 數組,因為有了 3D 數組之後,管理它們就有點困難了。
As we have understood the declaration, initialization and accessing of multidimensional arrays in PHP, it is time for a quick brief explanation with examples.
1. 2D Array in PHP
2D arrays are basically array inside another array. Consider a scenario that a user have 10 books and each book has a different name, cost, type. In this case, the programmer can create an array of book numbers and each element of the main array holds the array which contains details of the book like name, cost, and type.
Code:
<?php /* Multidimensional 2D array for 4 books and each book having a different array containing book name, cost and type. */ $books = array( array("Fiction ", "Action and Adventure ", 800), array("Fiction ", "Anthology ", 1000), array("Non- Fiction ", "Biography ", 600), array("Non- Fiction ", "Cook Book ", 900) ); /* Accessing of a 2D array with the row_number and column_number */ for ($row_num = 0; $row_num < 4; $row_num++) { echo "<p>Book number is $row_num"; for ($col_num = 0; $col_num "; } ?>
Output:
2. 3D Array in PHP
3D arrays are an extension of 2D arrays. 3D arrays contain one more dimension and provides the chance to add more detailed information. Consider a scenario of employee array, in which employee have name, company and year and each employee has a company profile with the attributes id, skills, and profile. Each employee has personal data also with the details of the city, state, and country. In Order, to Store, the Above Data 3D Array Would Be Required.
Code:
<?php $Employee = array(array(array("name", "company", "year"), array("id","skills","profile"), array("city","state","country") ), /* array to store the name, company and year of employee*/ array(array("jiya", "Infosys", 2016), array("ram", "ola", 2017) ), /* array to store the id, skills and profile of employees */ array(array("E101", "PHP", "developer"), array("E103", "mysql", "DBA") ), /* array to store the city, state and country of employees */ array(array("Bangalore", "Karnataka", "India"), array("San Francisco", "California", "USA") ) ); ?> <?php echo "<ul>"; for ( $outermost = 0; $outermost The outermost number $outermost"; echo "
- ";
for ( $row_num = 0; $row_num Now displaying the row number $row_num";
echo "
- ";
for ( $col_num = 0; $col_num ".$Employee[$outermost][$row_num][$col_num]."";
}
echo "
Output:
The above example clearly displays the details of the employee along with their skills in a very user-friendly manner. It allows the detailing of each and every employee in a fancy 3d arrays. We are dealing with 3d arrays, in order to access that, we need to first reach to the main array and then to the index which again holds the subarray and then to the elements of its subarray. In this way, accessing to the elements works in the case of multidimensional arrays starting from the outermost to the innermost array. similarly, in real life, there are sub-arrays or detailed things in which multidimensional arrays are used.
Conclusion
The above explanation clearly shows how the multidimensional arrays are used in php along with their basic syntax and initialization. Multidimensional arrays play an important role when it comes to working on real-life problems as they allow the user to store the data in a detailed form. Moreover, as shown above, php allows storing the multidimensional data either in indexed or associative form according to the requirements which makes it more friendly to access and store the data.
以上是PHP 中的多維數組的詳細內容。更多資訊請關注PHP中文網其他相關文章!

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。

在PHP中使用預處理語句和PDO可以有效防範SQL注入攻擊。 1)使用PDO連接數據庫並設置錯誤模式。 2)通過prepare方法創建預處理語句,使用佔位符和execute方法傳遞數據。 3)處理查詢結果並確保代碼的安全性和性能。

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP可以輕鬆創建互動網頁內容。 1)通過嵌入HTML動態生成內容,根據用戶輸入或數據庫數據實時展示。 2)處理表單提交並生成動態輸出,確保使用htmlspecialchars防XSS。 3)結合MySQL創建用戶註冊系統,使用password_hash和預處理語句增強安全性。掌握這些技巧將提升Web開發效率。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版

Dreamweaver CS6
視覺化網頁開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中