搜索
首页后端开发php教程PHP 中的多维数组

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

PHP 中的多维数组

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 "
    "; echo ""; } echo "
"; echo ""; } echo ""; ?>

Output:

PHP 中的多维数组

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中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP:服务器端脚本语言的简介PHP:服务器端脚本语言的简介Apr 16, 2025 am 12:18 AM

PHP是一种服务器端脚本语言,用于动态网页开发和服务器端应用程序。1.PHP是一种解释型语言,无需编译,适合快速开发。2.PHP代码嵌入HTML中,易于网页开发。3.PHP处理服务器端逻辑,生成HTML输出,支持用户交互和数据处理。4.PHP可与数据库交互,处理表单提交,执行服务器端任务。

PHP和网络:探索其长期影响PHP和网络:探索其长期影响Apr 16, 2025 am 12:17 AM

PHP在过去几十年中塑造了网络,并将继续在Web开发中扮演重要角色。1)PHP起源于1994年,因其易用性和与MySQL的无缝集成成为开发者首选。2)其核心功能包括生成动态内容和与数据库的集成,使得网站能够实时更新和个性化展示。3)PHP的广泛应用和生态系统推动了其长期影响,但也面临版本更新和安全性挑战。4)近年来的性能改进,如PHP7的发布,使其能与现代语言竞争。5)未来,PHP需应对容器化、微服务等新挑战,但其灵活性和活跃社区使其具备适应能力。

为什么要使用PHP?解释的优点和好处为什么要使用PHP?解释的优点和好处Apr 16, 2025 am 12:16 AM

PHP的核心优势包括易于学习、强大的web开发支持、丰富的库和框架、高性能和可扩展性、跨平台兼容性以及成本效益高。1)易于学习和使用,适合初学者;2)与web服务器集成好,支持多种数据库;3)拥有如Laravel等强大框架;4)通过优化可实现高性能;5)支持多种操作系统;6)开源,降低开发成本。

揭穿神话:PHP真的是一种死语吗?揭穿神话:PHP真的是一种死语吗?Apr 16, 2025 am 12:15 AM

PHP没有死。1)PHP社区积极解决性能和安全问题,PHP7.x提升了性能。2)PHP适合现代Web开发,广泛用于大型网站。3)PHP易学且服务器表现出色,但类型系统不如静态语言严格。4)PHP在内容管理和电商领域仍重要,生态系统不断进化。5)通过OPcache和APC等优化性能,使用OOP和设计模式提升代码质量。

PHP与Python辩论:哪个更好?PHP与Python辩论:哪个更好?Apr 16, 2025 am 12:03 AM

PHP和Python各有优劣,选择取决于项目需求。1)PHP适合Web开发,易学,社区资源丰富,但语法不够现代,性能和安全性需注意。2)Python适用于数据科学和机器学习,语法简洁,易学,但执行速度和内存管理有瓶颈。

PHP的目的:构建动态网站PHP的目的:构建动态网站Apr 15, 2025 am 12:18 AM

PHP用于构建动态网站,其核心功能包括:1.生成动态内容,通过与数据库对接实时生成网页;2.处理用户交互和表单提交,验证输入并响应操作;3.管理会话和用户认证,提供个性化体验;4.优化性能和遵循最佳实践,提升网站效率和安全性。

PHP:处理数据库和服务器端逻辑PHP:处理数据库和服务器端逻辑Apr 15, 2025 am 12:15 AM

PHP在数据库操作和服务器端逻辑处理中使用MySQLi和PDO扩展进行数据库交互,并通过会话管理等功能处理服务器端逻辑。1)使用MySQLi或PDO连接数据库,执行SQL查询。2)通过会话管理等功能处理HTTP请求和用户状态。3)使用事务确保数据库操作的原子性。4)防止SQL注入,使用异常处理和关闭连接来调试。5)通过索引和缓存优化性能,编写可读性高的代码并进行错误处理。

您如何防止PHP中的SQL注入? (准备的陈述,PDO)您如何防止PHP中的SQL注入? (准备的陈述,PDO)Apr 15, 2025 am 12:15 AM

在PHP中使用预处理语句和PDO可以有效防范SQL注入攻击。1)使用PDO连接数据库并设置错误模式。2)通过prepare方法创建预处理语句,使用占位符和execute方法传递数据。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脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。