PHP Arrays

PHP Arrays

An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

There are three different kind of arrays and each array value is accessed using an ID c which is called array index.

  • Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion.
  • Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
  • Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices.

PHP String

A string is a sequence of characters, like "Hello world!". A string can be any text inside quotes. You can use single or double quotes:

  •       
                            <?php
    $cars = array("Volvo", "BMW", "Toyota");
    echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
    ?>
                        

    What is an Array?

    An array is a special variable, which can hold more than one value at a time. An array can hold many values under a single name, and you can access the values by referring to an index number.

    Create an Array in PHP

    In PHP, the array( ) function is used to create an array:array( ); In PHP, there are three types of arrays: • Indexed arrays - Arrays with a numeric index • Associative arrays - Arrays with named keys • Multidimensional arrays - Arrays containing one or more arrays

  •       
                            <?php
    $x = 5985;
    var_dump($x);
        ?>
                        

    Loop Through an Indexed Array

    To loop through and print all the values of an indexed array, you could use a for loop, like this:

    Variable with global scope:

  •       
                            <?php
    $cars = array("Volvo", "BMW", "Toyota");
    $arrlength = count($cars);
    
    for($x = 0; $x < $arrlength; $x++) {
        echo $cars[$x];
        echo "";
    }
    ?>
                        

    PHP Associative Arrays

    Associative arrays are arrays that use named keys that you assign to them. There are two ways to create an associative array:

    PHP Array

    An array stores multiple values in one single variable. The PHP var_dump() function returns the data type and value:

    Variable with global scope:

    $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

    or:

  •       
                            <$age['Peter'] = "35";
    $age['Ben'] = "37";
    $age['Joe'] = "43";>
                        
  •       
                            <?php
    $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
    echo "Peter is " . $age['Peter'] . " years old.";
    ?<
                        

    Loop Through an Associative Array

    To loop through and print all the values of an associative array, you could use a foreach loop, like this.

  •       
                             <?php
    $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
    
    foreach($age as $x => $x_value) {
        echo "Key=" . $x . ", Value=" . $x_value;
        echo "";
    }
    ?>
                        

    PHP Multidimensional Arrays

    A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

    The dimension of an array indicates the number of indices you need to select an element.

    • For a two-dimensional array you need two indices to select an element
    • For a three-dimensional array you need three indices to select an element

    PHP - Two-dimensional Arrays

    A two-dimensional array is an array of arrays (a three-dimensional array is an array of arrays of arrays).

    Now the two-dimensional $cars array contains four arrays, and it has two indices: row and column. To get access to the elements of the $cars array we must point to the two indices (row and column):

  •       
                            <?php
    echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".";
    echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".";
    echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".";
    echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".";
        ?>
                        

    We can also put a for loop inside another for loop to get the elements of the $cars array (we still have to point to the two indices):

  •       
                            <?php
    for ($row = 0; $row < 4; $row++) {
        echo "Row number $row";
        echo "";
        for ($col = 0; $col < 3; $col++) {
        echo "".$cars[$row][$col]."";
        }
        echo "";
    }
    ?<
    

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1