Web Info and IT Lessons Blog...

Tuesday, 11 November 2014

PHP Arrays

The previous lesson of PHP lessons series was about If/Else statements and in this lesson we will learn about arrays in PHP. Arrays have a great importance in PHP and every other programming language. Usually a normal variable holds a single value like if we want to store the names of a few cities, we will need more than one variables to hold those values but the same task can be acheived by a single variable with array datatype. An array is a special type of variable which can hold one or more values in it.

PHP Arrays

How to create an array in PHP?

An array can be easily created by simply setting the data type of a variable equal to an array i.e


$cities = array();

Types of arrays in PHP?

There are three types of arrays in PHP:

1. Indexed Arrays
2. Associative Arrays
3. Multidimensional Arrays

Also Read: PHP Data Types

Indexed Arrays

There are two methods of adding values to an array. The first method is to add values to an array while we initialize(create) it i.e


$cities = array("Brazil","France","Argentina","Germany","Japan");

The second method is to add a value at every index of an array i.e


$cities = array();
$cites[0] = "Brazil";
$cites[1] = "France";
$cites[2] = "Argentina";
$cites[3] = "Germany";
$cites[4] = "Japan";

Both of the above mentioned methods of adding values to an array are examples of indexed arrays as the values are added to the indexes of the array.

Associative Arrays

Unlike indexed arrays, in an associative a value is added against a key not index. There are two ways to add values to an associative array. The first one is to add values while initializing i.e


$marks=array("John"=>"76","Sara"=>"63","Luna"=>"92"); 

In the above array the marks of students are stored against the names of the students. The second method of adding values to an associative array is by adding a value against the key after the array declaration i.e


$marks=array();
$marks['John'] = 76;
$marks['Sara'] = 63;
$marks['Luna'] = 92;

Multidimentional Arrays

A multidimentional array contains an array at every index of an array. What we mean here is that we have a main array and as we know that an array can store more than one values at it's indexes but this time we are not storing values at array indexes but we are saving arrays at every index. Example of a multidimentional array is shown below:


$cities=array
(
  array("New York","Washington DC","Las Vegas"),
  array("London","Birmingham","Bristol"),
  array("Dehli","Mumbai","Lucknow")
);

How to echo array values in PHP?

There are three types of arrays in PHP so there are three different methods to echo the values of arrays in PHP. In indexed arrays as the values are saved at different indexes of the array, so we will echo the value at the specific index of an array. Let us echo the value at third index of the array:


echo $cites[2] = "Argentina";

As the indexes in an array starts from 0, so $cites[2] becomes the third index.

In associative arrays the values are saved against the keys, so this is how we can echo the values of an associative arrays:


echo $marks['John'];

The above line will echo value of marks of John which is 76. Here John is the key of associative array.Now the indexes of a multidimentional are a bit complicated. In the multidimentional array shown below,


$cities=array
(
  array("New York","Washington DC","Las Vegas"),
  array("London","Birmingham","Bristol"),
  array("Dehli","Mumbai","Lucknow")
);

the value New York will be saved at the index $cities[0][0]. Similarly, all other cities will be saved at the following indexes:


$cities[0][0] = "New York";
$cities[0][1] = "Washington DC";
$cities[0][2] = "Las Vegas";
$cities[1][0] = "London";
$cities[1][1] = "Birmingham";
$cities[1][2] = "Bristol";
$cities[2][0] = "Dehli";
$cities[2][1] = "Mumbai";
$cities[2][2] = "Lucknow";

Now the echo the the value "Birmingham" of the array cities we will have to echo the index $cities[1][1] i.e


echo $cities[0][0];

No comments:

Post a Comment