Array in PHP

An array is a storage area where more than one values can be stored. In simple terms array is a collection of variables of same type. In PHP an array has mainly two things, Key (Index) and Value. Keys are referred to as index.  An array is a special variable, which can hold more than one value at a time.

There are three types of array.

  1. Indexed Array
  2. Multidimensional  Array
  3. Associative Array

Indexed Array

In PHP array is defined using a function array(). How will we create an array is specified below.

Syntax

$arrayname=array(value1,value2,…valueN)

For Ex.

$name=array("Pratik","Deepen","Jainam");

$age=array(20,30,43,12);

Here $name is a array of strings which contains names , whereas $age is an array of integer values. As you can see array() function is used for defining an array.

Note : when you don’t specify any index default indexing will be done which will start from 0,1,2… and so on. In our example $name[0] will store “Pratik” , $name[1] will store “Deepen” and so on.

Accessing Array elements

The simplest technique to access array element is through the index.

$arrayaname[element_index]

For ex. $name[0] = “Pratik“ ,$age[0]=20

<?php
       $name= array("Pratik","Deepen","Jainam","Rishi");
       echo "Names ".$name[0]." ".$name[1]." ".$name[2];
?>

Here $name[0] is used for accessing 1st element of array.

Associative Array

Associative array is one of the greatest inclusions in PHP. Associative array is an array which stores strings as key. An array which contains named key is known as associative array. 

Syntax

$arrayname=array(key1=>value1,key2=>value2,..keyN=>valueN);

Here you can use any string value in place of key.

For Ex.

$rno=array("Pratik"=>29, "Jainam"=>27, "Deep"=>30);

As you can see from the example here "Pratik" is a string value which is used as index(key) for value 29.

<?php
        $rno= array("Pratik"=>29,"Jainam"=>27,"Deep"=>30);
        echo "Roll No of Pratik is ".$rno["Pratik"];
?>

Output

Roll No of Pratik is 29

Use of foreach for traversing Associative Array

Well foreach loop is used for traversing through an associative array, the main reason for using foreach in place of for loop is because the iteration is not fix in associative array. Let’s learn it through an example.

<?php
       $rno= array("Pratik"=>29,"Jainam"=>27,"Deep"=>30);
       foreach($rno as $key=>$val)
       {             
              echo "Roll No of ".$key." is ".$val."<br>";
       }
?>

Here $rno is Array variable, $key will store one key from the array each time the loop is executed and $val will store one element every time the loop is executed. It will traverse through all array elements one by one and store the key in $key and value in $val.

Output

Roll No of Pratik is 29
Roll No of Jainam is 27
Roll No of Deep is 30

Finding the length of an Array

To find the length of an array (number of elements in array) count() function is used. The example for same is given below.

<?php
       $name=array("Pratik","Deep","Deepen","Jainam");
       $total=count($name);
       echo "Length of Array is ".$total;
?>

Output

Length of Array is 4

You can use the length of array for traversing a numeric index array.

Multidimensional Array

Multidimensional array is used when you want to store values with more than one key.  An array of arrays is known as 2D array. Ultimately multidimensional array is an array which contains one or more array.

Let’s look at one example to better understand multidimensional array.

<?php
        $student=array(array(1,"Pratik","Gujarat"),array(2,"Deep","Delhi"));
        echo "Roll No ".$student[0][0]." Name ".$student[0][1]." State ".$student[0][2]."<br>";
        echo "Roll No ".$student[1][0]." Name ".$student[1][1]." State ".$student[1][2]."<br>";
?>

Output

Roll No 1 Name Pratik State Gujarat
Roll No 2 Name Deep State Delhi

Here we have created an array named $student which contains 2 array as its element. In short it’s a 2D Array. Since the array has two dimensions (row & column) the elements will be accessed using the combination of row and column as $arrayname[row_index][column_index].

If you have noticed an array in PHP can store values of different data types , as you can see in the example we have stored roll no which is integer , name & address which are string in a single array.

We can also use foreach loop for accessing multidimensional array elements as shown below.

<?php
       $student=array(array(1,"Pratik","Gujarat"),array(2,"Deep","Delhi"));
       foreach($student as $row)
       {
               foreach($row as $col)
               {
                     echo $col." ";
               }
               echo "<br>";
       }             
?>

Output

1 Pratik Gujarat
2 Deep Delhi

So to conclude in this tutorial we have learnt basics of array and array types.

Share this Article on Social Media

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments