PHP Arrays

In PHP, Arrays are beneficial to handle large number of data with index. Benefit of index, we can retrieve back the information.

Why do we need Array?

Array are beneficial to keep large number of data in single variable. You would wonder and astonish to know, SQL, Classes, Online data fetching(JSON) etc. are strictly result of array.

The need of array can easily understand from following example.

Example:

Lets print five fruits name.

<?php

echo “Apple”;

echo “Mango”;

echo “Banana”;

echo “Jackfruit”;

echo “Papaya”;

?>

The above code is going to print the fruit names. Suppose some one asked me to print the third fruit name.

How would I print the third fruit name!!!

To print the third fruit name, I need some kind of reference to the fruit name which would say this is third fruit and its name is Banana. But here, such reference is missing. To solve the issue, Array is used. Lets see how to define an array

How to define an Array in PHP?

In two ways, we can define array.

Lets see the first one.

Array declaration using variable name with square bracket

Syntax: 

$variable_name[]=”value”;

Example:

Lets store five fruit names in the array.

<?php

$fruits[]=”Apple”;

$fruits[]=”Banana”;

$fruits[]=”Jackfruit”;

$fruits[]=”Grapes”;

$fruits[]=”Papaya”;

?>

Here $fruits is the array variable. If you notice, we have used square bracket with the variable name. This way, we have defined five array values. Apple, Banana, Jackfruit etc. are called array values or array data. The square bracket, Assigns unique index to each value. (In next step, what is index will be clarified)

In this array, we have stored five fruit names. Lets see how to store five integer values in an array.

<?php

$numbers[]=10;

$numbers[]=20;

$numbers[]=30;

$numbers[]=40;

$numbers[]=50;

?>

Here $numbers is an array, and we have put five number values(10 to 50) to it. In first example, we have kept fruit names in between double quotes but in second example, we have not kept the values in any quote. Because, when we provide string value, we need to keep inside single quote or double quote (which ever you prefer). In second example, we have provided integer value, so we have not used any quotes.

Lets see the other way of defining array

Array declaration using Array keyword:

The demerit of the above way of defining an array is, it is time consuming and effort consuming process. Because we need to type each line to set the value to the array. Using array keyword we can simply assign values in one single line.

Syntax:

$variable_name=array(value1, value2, value3,….,valueN);

Example:

Lets take same above example. Lets define fruit array using array keyword

<?php

$fruits=array(“Apple”,”Banana”,”Grapes”,”Jackfruit”,”Papaya”);

?>

Here $fruits is an array. Apple, Banana, Grapes etc. are values.

Lets define array with integer values.

<?php

$numbers=array(10,20,30,40,50);

?>

So far, we have learned, how to define array. Now lets see how to fetch data.

How to fetch data from an Array?

To fetch data from an array, we need to understand index first. Lets begin with the previous example.

Towards the beginning of this article, I have mentioned, just by printing fruit names, it was not easy to fetch required fruit name because reference was missing. Thats why array came into the place. By using an array, we can store any number of data and each data has unique reference. Using this reference we can fetch data from an array. This reference is known as index value.

<?php

$fruits=array(“Apple”,”Banana”,”Grapes”,”Jackfruit”,”Papaya”);

?>

In the above code:

The first fruit name is Apple

The second fruit name is Banana

The third fruit name is Grapes

The fourth fruit name is Jackfruit

The fifth fruit name is Papaya

Remember: In general daily life, we count from 1. If we have five elements. Then we count first element, second element, third element etc. But in Programming, Scripting World count begins from 0 (Zero). 

Therefore,

To refer first fruit name, we refer it as 0th fruit name=Apple

The first fruit name=Banana

The Second fruit name=Grapes

The third fruit name=Jackfruit

The fourth Fruit Name=Papaya

This is because, the count begins from 0 (Zero). (0,1,2,3,4,5…)

Such counts are referred as Index.

When I said the 0th fruit name is Apple, I meant, the 0 index of $fruits array is having value Apple.

The 1st index of $fruits array is having value Banana.

The Second index of $fruits array is having value Grapes.

And so on.

If an array has five values, then the index of first value is 0, the index of second value is 1, the index of third value is 2, the index of fourth value is 3 and the index of fifth value is 4.

Some one asked me to print the third fruit name.

Then which index value, I need to refer?

The index value of third fruit would be, 2.

To fetch data from an array using index number is as below:

<?php

$fruits=array(“Apple”,”Banana”,”Grapes”,”Jackfruit”,”Papaya”);

echo $fruits[2];

?>

No matter in which way you have defined the array (Either variable name square bracket array or using array keyword), to fetch the data, we need to provide the respective index number inside the square bracket.

To print first fruit, the code is $fruits[0];

To print second fruit, the code is $fruits[1];

To print third fruit, the code is $fruits[2];

And so on.

In the defined array, I have five fruit names.

To fetch last fruit name, the code is echo $fruits[4]

What if I provide 5 index?

<?php

$fruits=array(“Apple”,”Banana”,”Grapes”,”Jackfruit”,”Papaya”);

echo $fruits[5];

?>

The above code would throw an error: Notice: Undefined offset: 5 in /opt/lampp/htdocs/sample/index.php on line 2

So before fetching data from an array, we must have know the number of values or data present in the array. And the maximum index number should always be less than the total number of value or data present in the array.

How to count total number of values present in an Array?

To count total number of values or data present in an array, PHP is providing two functions.

sizeOf function

Lets count the number of values or data present in an array using sizeOf function.

<?php

$fruits=array(“Apple”,”Banana”,”Grapes”,”Jackfruit”,”Papaya”);

echo sizeOf($fruits);

?>

The above code would give 5.

Count function

Lets count the number of values or data present in an array using count function.

<?php

$fruits=array(“Apple”,”Banana”,”Grapes”,”Jackfruit”,”Papaya”);

echo count($fruits);

?>

The above code prints 5.

So before fetching data from an array, we must have to be aware of total number of data or values present in the array. The maximum index number that we pass to fetch array value should always be less than the total number of values or data present in array.

How to remove data from an Array?

So far, we have learned how to define array, fetch array values and count total number of data or values present in an array. Situation may appear, where we may want to empty the array.

In two ways we can empty an array:

First Solution:  Use unset function.

<?php

$fruits=array(“Apple”,”Banana”,”Grapes”,”Jackfruit”,”Papaya”);

unset($fruits);

?>

Second Solution: assign empty array

<?php

$fruits=array(“Apple”,”Banana”,”Grapes”,”Jackfruit”,”Papaya”);

$fruits=array();

?>

In either way, $fruits is empty now.