codescracker


php

PHP Arrays



« Previous Tutorial Next Tutorial »


PHP arrays are used to store multiple values in a single variable.

Create an Array in PHP

You have to use a function named array() to create an array in PHP. Here is the general form to create an array in PHP:

array();

PHP Arrays Example

Here is an example of arrays in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
$cars = array("Audi", "BMW", "Mercedes"); 
echo "I like these cars :<br/> " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

</body>
</html>

Here is the output of the above PHP script:

php arrays

Types of Arrays in PHP

There are following three types of arrays available in PHP:

PHP Indexed Arrays

There are two ways to create an indexed arrays in PHP. The index can be assigned automatically like this:

$cars = array("Audi", "BMW", "Mercedes");

Or, you can assign index manually like this:

$cars[0] = "Audi";
$cars[1] = "BMW";
$cars[2] = "Mercedes";

Note - Index always starts from 0

Here is an example, creates an indexed arrays named $cars, and assigns the three elements to it, and then prints a text containing the array values:

<!DOCTYPE html>
<html>
<body>

<?php
$cars = array("Audi", "BMW", "Mercedes");
echo "I like these cars :<br/>" . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

</body>
</html>

The above PHP script will produce the following output:

php indexed arrays

Get Length of an Array in PHP

To get the length of an array in PHP, just use count() function, returns the length (number of element) of the array. Here is an example, uses count() function to find the length of the array in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
$cars = array("Audi", "BMW", "Mercedes");
echo count($cars);
?>

</body>
</html>

Here is the output of the above PHP script:

3

Loop to display PHP Indexed Array

To loop through and print all the values of an indexed array in PHP, you could use a for loop as shown in this example:

<?php
$cars = array("Audi", "BMW", "Mercedes");
$arrlength = count($cars);
echo "I like these cars :"."<br/>";
for($x = 0; $x < $arrlength; $x++) {
    echo $cars[$x];
	if($x==1){
		echo " and ";
		continue;
	}
    echo ", ";
}
?>

It will display the same result as above which is:

php indexed arrays looping

PHP Associative Arrays

Associative arrays in PHP, are arrays that uses named keys that you assign to them. There are two ways to create an associative arrays in PHP. Here is the first way to create an associative arrays in PHP:

$age = array("Anoop"=>"18", "Harsh"=>"19", "Abhay"=>"20");

Here is the second way to create associative arrays in PHP:

$age['Anoop'] = "18";
$age['Harsh'] = "19";
$age['Abhay'] = "20";

The named keys can then be used in a script. Here is an example:

<?php
$age = array("Anoop"=>"18", "Harsh"=>"19", "Abhay"=>"20");
echo "Anoop " . $age['Anoop'] . " years old.";
?>

It will display the following result:

Anoop 18 years old.

Loop to display PHP Associative Arrays

Let's look at the following example:

<?php
$age = array("Anoop"=>"18", "Harsh"=>"19", "Abhay"=>"20");

foreach($age as $x => $x_value) {
    echo "Name=" . $x . ", Age=" . $x_value;
    echo "<br>";
}
?>

It will display the following output:

Name=Anoop, Age=18
Name=Harsh, Age=19
Name=Abhay, Age=20

PHP 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.

PHP Multidimensional Arrays Example

Let's look at the following example where we create a two dimensional array to store marks of three students in three subjects:

<html>
<body>
<?php
   $marks = array( 
		"Anoop" => array
		(
		"physics" => 89,	    
		"maths" => 93,	    
		"chemistry" => 88	    
		),
		"Harsh" => array
                (
                "physics" => 91,
                "maths" => 94,
                "chemistry" => 96
                ),
                "Abhay" => array
                (
                "physics" => 86,
                "maths" => 89,
                "chemistry" => 90
                )
	     );
   /* Accessing multi-dimensional array values */
   echo "Marks of Anoop in physics : " ;
   echo $marks['Anoop']['physics'] . "<br />"; 
   echo "Marks of Harsh in maths : ";
   echo $marks['Harsh']['maths'] . "<br />"; 
   echo "Marks of Abhay in chemistry : " ;
   echo $marks['Abhay']['chemistry'] . "<br />"; 
?>
</body>
</html>

Here is the output of this PHP script:

Marks of Anoop in physics : 89
Marks of Harsh in maths : 94
Marks of Abhay in chemistry : 90

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test