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();

Initialize an Array in PHP

Here are some sample ways to initialize an array in PHP.

// initialize an empty array in PHP
$array_var = array();

// shorthand notation of initializing an empty array in PHP 
$array_var = [];

// initialize a simple array with some values in PHP 
$tutorial_var = array('PHP', 'Array', 'One dimensional Array', 'Multidimensional Array');

// shorthand notation of initializing array with some values in PHP 
$tutorial_var = ['PHP', 'Array', 'One dimensional Array', 'Multidimensional Array'];

// initialize simple associative array in PHP 
$tutorial_var = array(
	'first' => 'PHP',
	'second' => 'Array', 
	'third' => 'One Dimensional Array',
	'fourth' => 'Multidimensional Array'
);

// initialize and set an array with key and value in PHP
$tutorial_var['second'] = 'Array';

// shorthand notation of initializing an array with some values in PHP 
$tutorial_var = [
	'first' => 'PHP',
	'second' => 'Array', 
	'third' => 'One Dimensional Array',
	'fourth' => 'Multidimensional Array'
];

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>
<head>
	<title>Getting Array Length Example in PHP</title>
</head>
<body>
<?php
	$cars = array("Audi", "BMW", "Mercedes");
	echo count($cars);
?>
</body>
</html>

Here is the output of the above PHP script:

get array length php

PHP Arrays Example

Here is an example of arrays in PHP:

<!DOCTYPE html>
<html>
<head>
	<title>PHP Array Example</title>
</head>
<body>
<?php
	// array example in PHP
	$cars = array('Audi', 'BMW', 'Mercedes');
	echo "I like these cars :<br/>";
	echo $cars[0].", ".$cars[1].", and ".$cars[2];
?>
</body>
</html>

Here is the output of the above PHP script:

php arrays

Below is one more example demonstrating array in PHP.

<!DOCTYPE html>
<html>
<head>
	<title>PHP Array Example</title>
</head>
<body>
<?php
	$tot = 5;
	for($i=0; $i<$tot; $i++)
	{
		$array_var[$i] = $i;
	}
	for($i=0; $i<$tot; $i++)
	{
		echo $array_var[$i]."<br/>";
	}
?>
</body>
</html>

The above PHP array example code will produce the output given below:

array in php

Let's take another example on array in PHP

<!DOCTYPE html>
<html>
<head>
	<title>PHP Array Example</title>
</head>
<body>
<?php
	$num = 2;
	for($i=1; $i<=10; $i++)
	{
		$array_var[$i] = $num * $i;
	}
	for($i=1; $i<=10; $i++)
	{
		echo $array_var[$i]."<br/>";
	}
?>
</body>
</html>

Here is the sample output of the above array example code

array example in php

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/>";
	echo $cars[0].", ".$cars[1]." and ".$cars[2];
?>
</body>
</html>

The above PHP script will produce the following output:

php indexed arrays

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:

<!DOCTYPE html>
<html>
<head>
	<title>Loop to display indexed array in PHP</title>
</head>
<body>
<?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;
		}
		if($x!=2)
		{
			echo ", ";
		}
	}
?>
</body>
</html>

Here is the sample output produced by the above indexed array example code in PHP.

php indexed arrays

PHP Indexed Array Example

Below is an example that demonstrates the indexed array in PHP.

<!DOCTYPE html>
<html>
<head>
	<title>PHP Indexed Array Example</title>
</head>
<body>
<?php
	$array_var = array("Java", "C", "C++", "PHP", "OS", "Networking", "everything");
	$array_length = count($array_var);
	for($i=0; $i<$array_length; $i++)
	{
		if($i==6)
		{
			echo "I just love to read ".$array_var[$i]." on CodesCracker<br/>";
		}
		else 
		{
			echo "I just love to read <b>".$array_var[$i]."</b> on CodesCracker<br/>";
		}
	}
?>
</body>
</html>

The output produced by the above indexed array example code is shown below:

php indexed array example

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:

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

It will display the following result:

php array example

Loop to display PHP Associative Arrays

Let's look at the following example:

<!DOCTYPE html>
<html>
<head>
	<title>Loop to display associative array in PHP</title>
</head>
<body>
<?php
	$age = array("Anoop"=>"18", "Harsh"=>"19", "Abhay"=>"20");
	foreach($age as $x => $x_value)
	{
		echo "Name=".$x.", Age=".$x_value;
		echo "<br/>";
	}
?>
</body>
</html>

It will display the following output:

associative array in php

PHP Associative Array Example

Here is an example illustrating associative array in PHP.

<!DOCTYPE html>
<html>
<head>
	<title>PHP Associative Array Example</title>
</head>
<body>
<?php
	$array_var = array(
		"1"=>"Java",
		"2"=>"C",
		"3"=>"C++",
		"4"=>"PHP",
		"5"=>"OS",
		"6"=>"Networking",
		"7"=>"everything"
	);
	$array_length = count($array_var);
	for($i=1; $i<=$array_length; $i++)
	{
		if($i==7)
		{
			echo "I just love to read ".$array_var[$i]." on CodesCracker<br/>";
		}
		else 
		{
			echo "I just love to read <b>".$array_var[$i]."</b> on CodesCracker<br/>";
		}
	}
?>
</body>
</html>

Below is the demo output produced by the above associative array example code in PHP.

php associative array example

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>
<head>
	<title>PHP Multidimensional Array Example</title>
</head>
<body>
<?php
	// initializing multidimensional array in PHP
	$array_var_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 multidimensional array in PHP
	echo "Marks of Anoop in Physics: ";
	echo $array_var_marks['Anoop']['Physics']."<br/>";
	echo "Marks of Harsh in Maths: ";
	echo $array_var_marks['Harsh']['Maths']."<br/>";
	echo "Marks of Abhay in Chemistry: ";
	echo $array_var_marks['Abhay']['Chemistry']."<br/>";
?>
</body>
</html>

Here is the output of this PHP script:

php multidimensional array

« Previous Tutorial Next Tutorial »


Latest 5 Posts by CodesCracker



Tools
Calculator

Quick Links
Signup - Login - Give Online Test