Let's talk!

Blogs » Technology » Multidimensional arrays in PHP

Multidimensional arrays in PHP

  • 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/3GYfOwb

Recent Blog Entries

  • A Server Virtualization Engineer partitions a physical server by using software into several smaller virtual servers to boost server resources. Besides, this individual also helps to ensure that space is saved, allowing many resources to use the same space. By doing so, they improve network performa...
  • October 29, 2018
    Posted by Deylr eylrod
  • We welcome the many gamers at our website; players can discover us to get cash of clans’ limitless gems and hacks. The coc hack tool is a strategy application for mobile device at present! We construct our coc hack tool for both android and iOS gadget, it is established by specialize industr...
  • One on the great "you was able to make it moments" to be described as a rookie must be in the event you notice your name employing a Madden video games. Would you play you because you in Madden? Would that certainly be considered a sign of narcissism reely more than the moment being proud of merely ...
  • The gaming companies are enduring a summer swoon, as being the industry-tracking NPD Group today announced its June retail sales figures, showing shrinking revenues year-over-year overall.Even with grim news with the industry overall, there was clearly some individual winners. Take-Two Interactive f...
View All