PHP is an object-oriented, open-source, interpreted scripting language that can be run on the server. PHP is an excellent web development language. As a result, it is used to build web apps.

  • PHP is a server-side scripting language for creating dynamic web applications that use the MySQL database.
  • It manages the website's dynamic content, database, and session monitoring.
  • PHP allows you to build sessions.
  • It has access to the cookies variable as well as the ability to set cookies.
  • It aids in the encryption and validation of data.
  • HTTP, POP3, SNMP, LDAP, IMAP, and several other protocols are supported by PHP.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

What Is Multidimensional Array in PHP

A multidimensional array in PHP containing one or more arrays is known as a multidimensional array. Multidimensional arrays of two, three, four, five, or more levels are supported by PHP. Most people, however, find it difficult to navigate arrays that are more than three layers long. The number of indices required to select an element is indicated by the array's dimension.

When we discuss storing values in PHP, we use the term list. There are two ways to accomplish the job of storing different values. The first method is to assign each value to a single variable, while the second method is to assign multiple values to a single variable, which is much more effective. An array is what we term it.

A multidimensional array in PHP is a data structure that allows you to store multiple values in a single variable. As a result, arrays are an integral component of the programming community's toolkit. Arrays can hold both numeric and string values, and they can be multidimensional.

In PHP, however, you'll need two indices for a two-dimensional array, three for a three-dimensional array, and so on to access an element in a multidimensional array. As a result, a multidimensional array is known as a collection of arrays or sub-arrays, depending on the situation.

An array's properties can be categorized as either numeric or associative.

  • A numeric array is one in which the elements contained in the array are accessed using numbers.
  • The elements contained in an associative array are accessed using strings or names. For instance, an employee's name may be used to gain access to information from the company's register or server.

Syntax:

array (

array (elements...),

array (elements...),

...

)

Dimensions

The number of indices needed to pick an element is indicated by the dimensions of a multidimensional array. To pick an element from a two-dimensional array, use two indices.

Two Dimensional Array

The simplest type of a multidimensional array is a two-dimensional array. It can be made with the aid of a nested array. The index of these arrays is always a number, and they can store any type of element. The index begins at zero by default.

Example:

<?php

// PHP program to create 

// multidimensional array

// Creating multidimensional

// array

$myarray = array(

    // Default key for each will

    // start from 0

    array("Ank", "Ram", "Sham"),

    array("Delhi", "Tripura", "Kanpur")

);      

// Display the array information

print_r($myarray);

?> 

Output

Multidimensional_Array_in_PHP_1 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Two Dimensional Associative Array

All associative arrays are similar to indexed arrays, except that instead of linear storage (indexed storage), each value may be associated with a user-defined string key.

Example

<?php

  // PHP program to creating two 

// dimensional associative array

$marks = array(      

    // Ankit will act as key

    "Ankit" => array(          

        // Subject and marks are

        // the key value pair

        "C" => 101,

        "DCO" => 65,

        "FOL" => 74,

    ),          

    // Ram will act as key

    "Ram" => array(

          // Subject and marks are

        // the key value pair

        "C" => 24,

        "DCO" => 55,

        "FOL" => 99,

    ),      

    // Anoop will act as key

    "Anoop" => array(          

        // Subject and marks are

        // the key value pair

        "C" => 88,

        "DCO" => 46,

        "FOL" => 99,

    ),

);  

echo "Display Marks: \n";      

print_r($marks);

?>

Output

Multidimensional_Array_in_PHP_2

Three Dimensional Array

It is a multidimensional array in PHP in its most basic form. Three-dimensional arrays have the same initialization as two-dimensional arrays. The difference is that as the number of dimensions grows, so does the number of nested braces.

Example

<?php  

// PHP program to creating three

// dimensional array  

// Create three nested array

$myarray = array(

    array(

        array(1, 2),

        array(3, 4),

    ),

    array(

        array(5, 6),

        array(7, 8),

    ),

);      

// Display the array information

print_r($myarray);

?>

Output

Multidimensional_Array_in_PHP_3.  

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Accessing Multidimensional Array Elements

In PHP, there are primarily two methods for accessing multidimensional array objects.

Dimensions such as array name[‘first dimension'][‘second dimension'] may be used to access elements.

  •       The for loop can be used to access elements.
  •       The for each loop can be used to access elements.

Example

<?php 

// PHP code to create 

// multidimensional array  

// Creating multidimensional

// associative array

$marks = array(      

    // Ankit will act as key

    "Ankit" => array(          

        // Subject and marks are 

        // the key value pair

        "C" => 95,

        "DCO" => 85,

        "FOL" => 74,

    ),          

    // Ram will act as key

    "Ram" => array(          

        // Subject and marks are 

        // the key value pair

        "C" => 44,

        "DCO" => 55,

        "FOL" => 99,

    ),      

    // Anoop will act as key

    "Anoop" => array(         

        // Subject and marks are

        // the key value pair

        "C" => 77,

        "DCO" => 88,

        "FOL" => 77,

    ),

);  

// Accessing the array element 

// using dimensions      

// It will display the marks of

// Ankit in C subject

echo $marks['Ankit']['C'] . "\n";      

// Accessing array elements using for each loop

foreach($marks as $mark) {

    echo $mark['C']. " ".$mark['DCO']." ".$mark['FOL']."\n"; 

}      

?> 

Output

Multidimensional_Array_in_PHP_4  

Conclusion

Multidimensional Array in PHP are arrays that instead of storing a single element, store another array at each index. To put it another way, multi-dimensional arrays should be described as an array of arrays. Every element in this array can be an array, and they can also hold other sub-arrays within them, as the name implies. Multiple dimensions may be used to view arrays or sub-arrays of multidimensional arrays. 

For any professional, to land a rewarding job in a reputed company, one must have a good grip on the subject and be well acquainted with the latest developments and technology upgrades. Enroll yourself in the Simplilearn Post Graduate Program in Full Stack Web Development course that will ensure expertise in several relevant topics such as multidimensional array in PHP and better job prospects. 

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449