目录
语法
如何在 PHP 中声明多维数组?
1. PHP 中二维数组的声明
2. PHP 中 3D 数组的声明
如何在 PHP 中初始化多维数组?
1.在 PHP 中初始化二维数组
2.在 PHP 中初始化 3D 数组
在 PHP 中访问多维数组
类型
2. 3D Array in PHP
Conclusion
首页 后端开发 php教程 PHP 中的多维数组

PHP 中的多维数组

Aug 29, 2024 pm 12:44 PM
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:

<!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:

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:

<!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:

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

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1653
14
CakePHP 教程
1413
52
Laravel 教程
1304
25
PHP教程
1251
29
C# 教程
1224
24
适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

在PHP API中说明JSON Web令牌(JWT)及其用例。 在PHP API中说明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

您如何在PHP中解析和处理HTML/XML? 您如何在PHP中解析和处理HTML/XML? Feb 07, 2025 am 11:57 AM

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

解释PHP中的晚期静态绑定(静态::)。 解释PHP中的晚期静态绑定(静态::)。 Apr 03, 2025 am 12:04 AM

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

php程序在字符串中计数元音 php程序在字符串中计数元音 Feb 07, 2025 pm 12:12 PM

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

PHP和Python:比较两种流行的编程语言 PHP和Python:比较两种流行的编程语言 Apr 14, 2025 am 12:13 AM

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

什么是PHP魔术方法(__ -construct,__destruct,__call,__get,__ set等)并提供用例? 什么是PHP魔术方法(__ -construct,__destruct,__call,__get,__ set等)并提供用例? Apr 03, 2025 am 12:03 AM

PHP的魔法方法有哪些?PHP的魔法方法包括:1.\_\_construct,用于初始化对象;2.\_\_destruct,用于清理资源;3.\_\_call,处理不存在的方法调用;4.\_\_get,实现动态属性访问;5.\_\_set,实现动态属性设置。这些方法在特定情况下自动调用,提升代码的灵活性和效率。

PHP:网络开发的关键语言 PHP:网络开发的关键语言 Apr 13, 2025 am 12:08 AM

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

See all articles