An array is a data structure that stores one or more similar type of values under a single name, and you can access the values by referring to an index number.

For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

In PHP, There are three different kind of arrays and each array value is accessed using an ID c which is called array index.

  • Indexed arrays – An array with a numeric index. Values are stored and accessed in linear fashion.
  • Associative arrays – An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
  • Multidimensional arrays – An array containing one or more arrays and values are accessed using multiple indices.

 


PHP Indexed Arrays:

These arrays can store numbers, strings and any object but their index will be represented by numbers.

There are two ways to create indexed arrays:

The index can be assigned automatically (index always starts at 0):

$numbers=array(“num1″,”num2″,”num3”);

or the index can be assigned manually:

$numbers[0]=”num1″;
$numbers[1]=”num2″;
$numbers[2]=”num3″;

 

Example:

Following is the example showing how to create and access numeric arrays.Here we have used array() function to create array.

<?php
/* First method to create an array. */
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $value )
{
   echo “Value is $value <br />”;
}
/* Second method to create an array. */
$numbers[0] = “one”;
$numbers[1] = “two”;
$numbers[2] = “three”;
$numbers[3] = “four”;
$numbers[4] = “five”;
foreach( $numbers as $value )
{
   echo “Value is $value <br />”;
}
?>

Output:
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
Value is one
Value is two
Value is three
Value is four
Value is five

 


PHP Associative Arrays :

The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.

There are two ways to create an associative array:

$results=array(“student1″=>”45%”,”student2″=>”65%”,”student3″=>”85%”);

or:

$results[‘student1’]=”45%”;
$results[‘student2’]=”65%”;
$results[‘student3’]=”85%”;

 

Example:

<?php
/* First method to associate create array. */
$results=array(“student1″=>”45%”,”student2″=>”65%”,”student3″=>”85%”);
echo “Result of Student1 is “. $results[‘student1’] . “<br />”;
echo “Result of Student2 is “. $results[‘student2’] . “<br />”;
echo “Result of Student3 is “. $results[‘student3’] . “<br />”;
/* Second method to create array. */
$results[‘student1’] = “low percentage”
$results[‘student1’] = “medium percentage”
$results[‘student1’] = “high percentage”
echo “Result of Student1 is “. $results[‘student1’] . “<br />”;
echo “Result of Student2 is “. $results[‘student2’] . “<br />”;
echo “Result of Student3 is “. $results[‘student3’] . “<br />”;
?>

Output:
Result of Student1 is 45%
Result of Student2 is 65%
Result of Student3 is 85%
Result of Student1 is low percentage
Result of Student2 is medium percentage
Result of Student3 is high percentage

 


Multidimensional Arrays :

A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.

Example:

In this example we create a two dimensional array to store marks of three students in three subjects:

This example is an associative array, you can create numeric array in the same type.

<?php
$marks = array(
                        “student1” => array
                        (
                            “physics” => 40,
                            “maths” => 30,
                            “chemistry” => 39
                        ),
                       “student2” => array
                        (
                            “physics” => 30,
                            “maths” => 45,
                            “chemistry” => 29
                         ),
                        “student3” => array
                         (
                             “physics” => 50,
                             “maths” => 35,
                             “chemistry” => 39
                         )
                      );
/* Accessing multi-dimensional array values */
echo “Marks for student1 in physics : ” ;
echo $marks[‘student1’][‘physics’] . “<br />”;
echo “Marks for student2 in maths : ” ;
echo $marks[‘student2’][‘maths’] . “<br />”;
echo “Marks for student3 in chemistry : ” ;
echo $marks[‘student3’][‘chemistry’] . “<br />”;
?>

Output:
Marks for student1 in physics : 40.
Marks for student2 in maths : 45.
Marks for student3 in chemistry : 39.

 


Get The Length of an Array – The count() Function :

The count() function is used to return the length (the number of elements) of an array.

Example:

<?php
$students=array(“student1″,”student2″,”student3”);
echo count($students);
?>

Output:3