多维数组没什么特别的,只不过是另一个数组中的一个数组。数组的每个索引保存另一个数组而不是单个元素,该元素又可以指向另一个数组或特定元素。使用从外部数组开始并向内部数组移动的多个维度来访问数组内部的这些子数组。维度基本上是访问或存储数组中特定位置的值所需的索引。 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中文网其他相关文章!