More from Amrit Singh

  • 0 comments, 0 likes
  • 0 comments, 0 likes
  • 0 comments, 0 likes

More in Politics

  • 0 comments, 24,466 views
  • 0 comments, 5,558 views
  • 0 comments, 4,889 views

Related Blogs

  • 0 comments, 0 likes
  • 0 comments, 0 likes
  • 0 comments, 0 likes

Archives

Social Share

Multidimensional arrays in PHP

Posted By Amrit Singh     December 21, 2021    

Body

PHP array functions are used to create an array(). PHP provides various array functions that help you access and modify the array’s elements. PHP array functions can be divided into three major types:

  • Indexed arrays - Arrays having a numeric index
  • Associative arrays - Arrays with named keys
  • Multidimensional arrays - Arrays containing more than one arrays

So, let’s get started in brief about multidimensional arrays and related examples.

What are multidimensional arrays?

Generally, arrays are described as a single list of key/value pairs. However, there are instances when you need to store values with one or more keys. In such cases, you need to use multidimensional arrays.

A multidimensional array is a PHP array function that contains one or more arrays. PHP supports multidimensional arrays that are usually two, three, four, or more levels deep. However, managing more than three levels of arrays can be complex for most people.

In the multidimensional PHP array function, every element in this array can be an array with several sub-arrays under them. Using multiple dimensions, you can access these arrays or sub-arrays in multidimensional arrays. Here, dimensions indicate the number of indices required to select an element. For example, for a two-dimensional array, you need two indices to select an element. Similarly, you need three indices for a three-dimensional array to select an element.

Two-dimensional array

It is the simplest form of a multidimensional array that can be created using a nested array. These types of arrays can be used to store any kind of element, but the index is always in a numeric form. The default starting point of index numbers is zero. 

Syntax:

array (

    array (elements...),

    array (elements...),

    ...

)

Example:

<?php

// PHP program to create 

// multidimensional array

// Creating multidimensional

// array

$myarray = array(

    // Default key for each will

    // start from 0

    array("Geeta", "Manish", "Raj"),

    array("Haridwar", "Lucknow", "Jaipur")

);

// Display the array information

print_r($myarray);

?>

Output:

Array

(

    [0] => Array

        (

            [0] => Geeta

            [1] => Manish

            [2] => Raj

        

    [1] => Array

        (

            [0] => Haridwar

            [1] => Lucknow

            [2] => Jaipur

        

)

Three Dimensional Array

Another form of a multidimensional array, three-dimensional arrays, requires similar initialization as that of two-dimensional arrays.  The only difference between the two is that as the number of dimensions increases, the number of nested braces also goes up. 

Syntax:

array (

    array (

        array (elements...),

        array (elements...),

        ...

    ,

    array (

        array (elements...),

        array (elements...),

        ...

    ,

    ...

)

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:

Array

(

    [0] => Array

        (

            [0] => Array

                (

                    [0] => 1

                    [1] => 2

                

            [1] => Array

                (

                    [0] => 3

                    [1] => 4

                

)

    [1] => Array

        (

            [0] => Array

                (

                    [0] => 5

                    [1] => 6

                

            [1] => Array

                (

                    [0] => 7

                    [1] => 8

                

        

)

How to access multidimensional array elements?

You can access multidimensional array elements in the following ways: 

  • By using dimensions as array_name [‘first dimension’][‘second dimension’].
  • By using a for a loop.
  • By using for each loop.

If you are well-versed in using PHP array functions, Eiliana provides you with relevant projects and opportunities. At Eiliana, you can work with quality clients while being 100% anonymous.

Blog Source:- https://bit.ly/3J7DLTK

Map

Location Information

  • Location: Delhi, India - Get Directions
  • Formatted Address: Delhi, India
  • Street Address: Delhi
  • State: Delhi
  • Country: India