1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Arrays in PHP

Discussion in 'PHP' started by MinalS, Nov 14, 2015.

  1. The elements in array are stored in an ordered format. The mapping of the elements is in key value pair. User can define the length of the array. There are different types of arrays in PHP.
    They are as listed below:
    1. Numeric array: It defines an array with a numeric index. User can access and save the values in linear order.
    2. Associative array: The array has strings assigned as the index. The values for the elements are saved in the key. The linear index is not used.
    3. Multidimensional array: There are more than one arrays in a multidimensional array. Multiple indexes are used for accessing the array values.

    Numeric array



    In PHP, a numeric array is useful for adding strings, numbers, and an object. The index value starts from 0. User can create the numeric array in two different ways as shown below.

    Example:

    Code:
    1) 
    <html>
        <body>
            <?php
                $num = array ( 5,10,15,20,25);
                foreach ( $num as $value )
                {
                    echo "The value for the element is $value";
                }
            ?>
        </body>
    </html>
    2)
        <html>
            <body>
                <?php
                    $num [0] = "five";
                    $num [1] ="ten";
                    $num [2] ="fifteen";
                    $num [3] ="twenty";
                    $num [5] ="twenty";
                    foreach ( $num as $value )
                    {
                        echo "The value for the element is $value";
                    }
                ?>
            </body>
        </html>
    
    User can use the count() function to calculate the length of the array in PHP.

    Example:

    Code:
    <html>
        <body>
            <?php
                $names = array ("Sam", "Dick", "Harry");
                echo count ($names);
            ?>
        </body>
    </html>
    
    Looping in an index array is possible using a for loop.

    Example:

    Code:
    <html>
        <body>
            <?php
                $names = array ("Sam", "Dick", "Harry");
                $length = count ( $names);
                for ( $a = 0; $a < $length; $a++)
                {
                    echo $names [$a];
                    echo "<br>";
                }
            ?>
        </body>
    </html>
    

    Associative array



    An associative array is used as a numeric array. The index reference is different as compared to the numeric array. The array contain string as their index. The association is possible using the key value pair.

    User can declare the array in two different ways.
    1. $result = array ("Sam" => "50", "Harry" => "70", "Dick" => "60" );
    2. $result[‘Sam’] ="50"; $result ["Harry"] = "70"; $result["Dick"] ="60";
    Example:
    Code:
    <html>
        <body>
            <?php
                $result = array ("Sam" => "50", "Harry" => "70", "Dick" => "60" );
                echo "The result of Sam is" . $result[‘Sam’] . ";
            ?>
        </body>
    </html>
    
    Looping is possible using the associative array. The foreach loop is added for the looping.

    Example:

    Code:
    <html>
        <body>
            <?php
                $result = array ("Sam" => "50", "Harry" => "70", "Dick" => "60" );
                foreach ( $result as $a => $a_value )
                {
                    echo "Key=" .$a. ", Value=" .$a_value;
                    echo "<br>";
                }
            ?>
        </body>
    </html>
    

    Multidimensional array



    A multidimensional array, an element in an array can be an array. Multiple index can be used for accessing the array elements. An array can contain more than one array.

    Example:

    Code:
    <html>
        <body>
            <?php
            $names = array 
            (
                array ("Sam", 20, 30 );
                array ("Henry", 40, 60);
                array ("Peter", 30, 70);
            );
            //Accessing the array elements
            echo $names[0][0].": Age:  $.names[0][1].", houseno: ".$names[0][2].".<br>";
            echo $names[1][0].": Age: $names[1][1].", houseno: ".$names[1][2].".<br>";
            echo $names[2][0].": Age: $names[2][1].", houseno: ".$names[2][2].".<br>";
            ?>
        </body>
    </html>    
    
     
    shabbir likes this.

Share This Page