Sponsored Links :
  • PHP

    Arrays

    There are times in programming where you need to keep track of many variables at once. Maybe the current time of day, the number of items in a package or the name of a certain product. These variables may all be very similar too though, you might, for example, need to keep track of the names of all employees in a company, or the number of items inside each of a large number of packages. Arrays can be used to keep track of these multiple values from within one variable.

    Arrays are a special type of data structure in PHP. They are used to hold multiple values, in a way which allows the values held within to be re-arranged, searched or sorted. Values can also be removed or added easily. Arrays are especially useful when you’re dealing with lists of items, or collections of values that need to be sorted into ascending or descending order.

    Creating and manipulating arrays

    An array is initialised using the language construct array(), and is assigned to a PHP variable name, in exactly the same way as other scalar variables [reference scalar in previous tut]. Arrays can contain any number of values, each of which can be any of the PHP data types. Here is an example of a simple array

    $array = array(1, "two", 3.0, "four");

    This will initialise the variable $array as an array containing the values 1, “two”, 3.0 and “four”.

    An array like this is known as a Non-associative array because the values are not associated with any specific keys but are instead just numbered numerically, like so…

    An example of a simple non-associative array

    An example of a simple non-associative array

    Displaying Arrays

    Once this array has been initialised, we can check the contents of the array in certain ways

    1
    2
    3
    
    echo $array;
    print_r ($array);
    var_dump($array);

    This produces

    1
    2
    3
    
    Array
    Array ( [0] => 1 [1] => two [2] => 3 [3] => four )
    array(4) { [0]=> int(1) [1]=> string(3) "two" [2]=> float(3) [3]=> string(4) "four" }

    Echo is not built to handle complex data structures like arrays. Instead we need to turn to our old friend print_r() - but even then, we’re not getting the full picture. Var_dump() produces the most information about our array, giving us the datatype held in each position of the array.

    Associative arrays

    Note the numbers in square brackets on lines 2 and 3 in the above example. These numbers correspond to the “keys” of each item in the array. Arrays work by assigning each item a “key-value pair” by which to reference each position in the array. In the above example, the keys are generated automatically, in sequence according to the order in which the values were entered. We can over-ride this behaviour though, and specify our own keys for each value

    $array = array(4 => 1, 3 => "two", 2 => 3.0, 1 => "four");

    Here, we’ve assigned the entries in the array to keys in descending numerical order.

    Another example is an array containing a list of employee names, and their associated job titles

    Now, you might be asking, what’s the use of this feature in real code? Well, this gives me a good opportunity to introduce a powerful feature of arrays - sorting.

    <<>>

Leave a Comment

Want to ask a question about anything in this tutorial? Have you spotted an inaccuracy, or noticed areas for improvement? Fancy just having a chat? Leave your comments below...

Recommended Reading from Amazon.com

Previous Tutorial
Variables in PHP


Next tutorial
Conditional Statements in PHP