PHP - Arrays

    PHP - Arrays

    Arrays are a type of data structure that allows you to store a number of elements having the same data type using a variable. By using an array the use of multiple variables for each value can be avoided. Arrays are easy to use and parse using an index value. You can easily handle when there is a fewer number of variables but when the count reaches 100 or more than that, arrays become the right choice to make. Array() is used to create an array. PHP supports three types of different arrays-

    • Indexed or numeric array
    • Associative array
    • Multidimensional array

    1. Indexed or numeric array

    You can define an array of any data type and the values are contained within it. But these values or elements of an array can be accessed using an indexed or key which is a number whose value starts from zero. It means if we want to access the first element of an array it is referred to as the “0th” element of the array. You can traverse an array with the help of a loop using an index. You can assign value to array in two ways as follows. In the first method, we define an array with complete value at once while in the second method we assign values to the array individually.

    Example

    <?php 
    // First method 
    $name_list = array("Jacob", "Anthony", "Rita", "Ayush", "Megha"); 
    // Accessing array elements 
    echo $name_list[1], "<br>"; 
    echo $name_list[0], "<br>"; 
    echo $name_list[3], "<br>"; 
    
    // Second Method
    $namelist[0] = "Jacob"; 
    $namelist[1] = "ANTHONY"; 
    $namelist[2] = "Ayush"; 
    $namelist[3] = "Rita"; 
    $namelist[4] = "Megha"; 
    // Accessing array elements 
    echo $namelist[2], "<br>"; 
    echo $namelist[0], "<br>"; 
    echo $namelist[3], "<br>"; 
    ?>

    Output Anthony Jacob Ayush Ayush Jacob Rita

    2. Associative arrays

    These arrays work similarly to the numeric arrays but can be accessed differently. In this array, the elements are assigned to a string type user-defined key which will create a strong relationship between keys and their values. Whenever you try to print the values of these string type values, do not use double quotes else no value will be returned.

    Example

    <?php
    $name = array("Zack"=>"Jacob", "Anthony"=>"Ayush",  
                      "Ram"=>"Rita", "Salim"=>"Sita",  
                      "Raghav"=>"Rani"); 
    // Accessing the array elements
    echo $name["Zack"], "<br>"; 
    echo $name["Salim"], "<br>";    
    ?>

    Output Jacob Sita

    3. Multidimensional arrays

    With the help of a multidimensional array, you can store different arrays at each index and you can also call them array within another array. You can access the elements of a multidimensional array using multiple dimensions.

    Example-

    <?php 
    $main_array = array( 
        array( 
            "name" => "Jacob Punk", 
            "email" => "Jacobpunk@gmail.com"
        ), 
        array( 
            "name" => "Ayush Smith", 
            "email" => "Ayushsmith@gmail.com"
        )
    );
    // Accessing multidimensional array elements 
    echo "Jacob Punk email-id is: " . $main_array[0]["email"]; 
    ?>

    Output

    Jacob Punk email-id is: Jacobpunk@gmail.com

    People are also reading: