PHP Arrays

There is a slight different in creation of arrays in functional programming languages and PHP. PHP uses the function array() to create an array of objects. An array in PHP is an ordered Map. It is a map of key value pairsm where the key is a string and the value can be anything like a string or even another array. Using this very idea of key-value pair, you can create multi dimensional arrays.

Let’s explore the simplest form of arrays containing a sequence of numbers.


$numbers = array (1,2,3);
print $numbers[0]."n";
print $numbers['0']."n";

print $numbers[1]."n";
print $numbers['1']."n";


As you can see, the array function takes as input a comma separated input. As mentioned before, an array is an ordered map. Since in this case, we have not specified any keys, the default numbering or index mechanism is used. That is the first element is accessed either by the key [0] or [‘0’]

$numbers = array (1,2,3);

The output can be clearly visualized

    [0] => 1
    [1] => 2
    [2] => 3

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s