多維數組沒什麼特別的,只不過是另一個數組中的一個數組。數組的每個索引保存另一個數組而不是單個元素,該元素又可以指向另一個數組或特定元素。使用從外部數組開始並向內部數組移動的多個維度來存取數組內部的這些子數組。維度基本上是存取或儲存數組中特定位置的值所需的索引。 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 允許使用關鍵字「array」以非常簡單的方式在 PHP 中聲明多維數組。為了在另一個數組中聲明數組,我們需要添加關鍵字“array”,然後添加該數組的元素。
代碼:
<?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"), ); ?>
上面顯示的第二種方法是常用的,因為它很容易理解。
代碼:
<?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 中初始化多維數組就像聲明一樣非常簡單。唯一要記住的是初始化子數組時使用大括號。在初始化多維數組中的值時,主數組可以是索引數組或關聯數組,在下面給出的範例中,主數組是具有鍵的關聯數組,如 Levis、Lee、Denizen 等,
代碼:
<?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 ) ); ?>
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 中常用的 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.
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:
<!DOCTYPE html> <html> <body> <?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</p>"; for ($col_num = 0; $col_num < 3; $col_num++) { // Accessing a particular element in a 2D array echo $books[$row_num][$col_num]; } echo "<br>"; } ?>
Output:
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:
<!DOCTYPE html> <html> <body> <?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 < 3; $outermost++ ) { echo "<li>The outermost number $outermost"; echo "<ul>"; for ( $row_num = 0; $row_num < 2; $row_num++ ) { echo "<li> Now displaying the row number $row_num"; echo "<ul>"; for ( $col_num = 0; $col_num < 3; $col_num++ ) { // accessing the array elements in a 3D array echo "<li>".$Employee[$outermost][$row_num][$col_num]."</li>"; } echo "</ul>"; echo "</li>"; } echo "</ul>"; echo "</li>"; } echo "</ul>"; ?> </body> </html>
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.
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中文網其他相關文章!