Multidimensional Array in PHP
A multidimensional array is nothing extraordinary but an array inside another array. Each index of the array holds another array instead of a single element which again can be pointing to another array or particular elements. These sub-arrays inside the array are accessed using the multiple dimensions starting from the outer array and moving towards the inner array. Dimensions are basically the indices that are required to access or store the value at a particular position in an array. Multidimensional arrays in php are highly used in real-time applications but it is quite tricky to deal with them as a comparison to single dimensional arrays because of the multiple brackets and some complexity in order to work with them, either accessing or storing values at a particular index, use of loops are required.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax
Below given is the general syntax of multidimensional arrays in PHP. Though multidimensional arrays in PHP can be 2D, 3D, 4D, and so on. The more dimensional array it is, the more difficult it is to manage them and more are the brackets added in front of the array name.
Syntax for 2D Array:
1 2 3 4 5 |
|
Syntax for 3D Array:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
How to Declare Multidimensional Arrays in PHP?
PHP allows its multidimensional arrays to be either indexed or associative. Associative arrays are more interactive as compared to the indexed one. PHP allows a very simple way to declare a multidimensional array in PHP using the keyword ‘array’. In order to declare an array inside another array, We need to add the keyword ‘array’ and then the elements of that array.
1. Declaration of 2D Array in PHP
Code:
1 2 3 4 5 |
|
OR
1 2 3 4 5 6 |
|
The second Method Shown Above Is Commonly Used as It Is Quite Easy To Understand.
2. Declaration of 3D Array in PHP
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
The above declaration is purely indexed one of 3D arrays as there are no key-value pairs used for the association.
How to Initialize a Multidimensional Array in PHP?
Initializing a multidimensional array means assigning the values or elements at the particular position or indices of an array. Initializing a multidimensional array in PHP is quite easy like declaring. The only thing to keep in mind is the use of braces while initializing the subarrays. While initializing the values in a multidimensional array, the main array can be indexed or associative, in the example given below, the main array is the associative one having the keys Like Levis, Lee, Denizen, Etc.,
1. Initializing 2D Array in PHP
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
2. Initializing 3D Array in PHP
Initialization of 3D Arrays is the Same as the 2D arrays, the only difference between the two is the dimensions. The 3D Array requires 1 more index to initialize it than a 2D Array. The number the dimensions of array increases, the number of indices to initialize it also increases. In the example below, the main array is a simple indexed array having sub-arrays in itself. We can also make the main array in the below example as associative like we have done in a 2D array with the key as the brand name which makes it easier for the customer to understand while accessing and storing it.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
Accessing Multidimensional Arrays in PHP
Accessing of multidimensional arrays in PHP is very simple and is done by using either the for or for each loop which is the commonly used loops in PHP. For the indexed arrays, accessing of array elements can be done normally using the row and column number similar to other languages like C, Java, Etc. (arr[row_Num][column_Num]).
In the case of associative arrays, accessing of the elements of a multidimensional array is done using the key and value Pairs (key => Value). Though the elements are accessed through the simple for or for each loop. Please refer to the below-given example for a clear understanding of the accessing of elements in multidimensional arrays.
Types
There is no particular state till which the multidimensional arrays can exist in a PHP. It depends on the particular situation and scenario. Dimensions of an array varies accordingly. Normally programmers use 2D and 3D arrays because, after 3D arrays, it is a bit difficult to manage them.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
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.
The above is the detailed content of Multidimensional Array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
