ARRAY in PHP Beginners guide

In this article we are going to discuss ARRAY in PHP Beginners guide. Arrays are wonderful ways to organize and use data in PHP. Array is Collection of different variables under the same label to keep values organized and easily accessible for processing. Here’s a quick example of an array of types of transportation:

$transportation = array( ‘Planes’, ‘Trains’, ‘Automobiles’ );

We use $ sign to give an array name like we do for variables. after that an equal sign and then keyword ‘array’ that tells the parser that we are working with arrays and then different values within parenthesis and each value enclosed in double or single quotes separated by comma.

Printing Array Items

$Top3Sites = array ("","","");
The output of the above programme will be.
[0] =>
[1] =>
[2] =>

Please note that we use print_r to print an array because you cannot print an array with echo or print function (both are used to display output) though you can use echo or print to display single items from the array e.g.:
echo $Top3Sites[1]; //
Remember that the index number starts from 0 and not 1.
Each value of the array get a unique ID which is known as INDEX NUMBER.

Types Of Arrays:

There are three different types of arrays in PHP:
a) Numeric Array: An array with a numeric ID key.
b) Associative Array: An array where each ID key is associated with a value.
c) Multidimensional Array: An array containing one or more arrays.

Numeric Arrays:

Numeric arrays use integer / numbers as their index number to identify each item of the array. The example we discussed above are numeric arrays as they have integer values as index numbers for each item.

Associative Arrays:

Sometimes it’s better to use the index name instead of index number. When you submit a form using POST or GET method you get associative array on the receiving page that contains the name of each form field as array index and its value as index value. Associative Arrays are more easy to handle and to process information especially dealing with complex form submission and dynamic values from database etc.

Multidimensional Arrays:

A multidimensional array is one where the items in an array are themselves arrays. Here’s an example:

$staff = array(
0 => array(
[‘Name’] => ‘Ricky Negi’,
[‘Position’] => ‘Reader’
1 => array(
[‘Name’] => ‘Swati Katoch’,
[‘Position’] => ‘Writer’

There we have a $staff array with multiple people in it. We could do multiple foreach functions to iterate over each of those people like this:

foreach( $staff as $key => $person ) {
        foreach( $person as $attribute => $value ) {
            echo<li>. $attribute .:. $value .</li>;

In the above example I looped through each person, and within each person I looped through their attributes and printed them. You can make up whatever variable names you wish, there’s nothing magic about the names. I tried to name mine in a way that made sense according to the contents.

Arrays are a very deep topic and it has a lot more to discuss and even I can easily write a complete book on arrays only. But the aim of this article is to give you very sound understanding of arrays and different methods to store and process information in arrays.

  • atif

    Well. You covered multidimensional array with examples. Array is such a topic which should be described much in detail. You should add examples of numeric and associative arrays as well..