logo
PHP Arrays
An array is a data structure that stores one or more similar type of values in a single variable.
Basic Syntax :
<!DOCTYPE html>
<html>
<head>
	<title>PHP Arrays - Basic Syntax</title>
</head>

<body>

<?php
$books = array("HTML5", "CSS3", "JavaScript", "Bootstrap", "PHP");
echo "<b>Web Development Books :</b>  " .$books[0]. ", " .$books[1]. "," .$books[2]. ", " .$books[3]. " and " .$books[4].".";
?>


</body>
</html>
Output :

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 array : An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.

Multidimensional array : An array containing one or more arrays and values are accessed using multiple indices

Indexed (or) Numeric arrays

An indexed or numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.

Following is the example showing how to create and access indexed (or) numeric arrays.

<!DOCTYPE html>
<html>
<head>
	<title>PHP Indexed (or) Numeric arrays</title>
</head>

<body>

<?php
$books = array("HTML5", "CSS3", "JavaScript", "Bootstrap", "PHP");
echo "<b>Web Development Books :</b>  " .$books[0]. ", " .$books[1]. "," .$books[2]. ", " .$books[3]. " and " .$books[4].".";
?>
<hr />
<p>PHP Array - <b>count()</b> Function</p>
<?php 
	echo count($books);
?>


</body>
</html>
Output :
Loop Through an Indexed Arrays
To loop through and print all the values of an indexed array, you could use a for loop, like this:
<!DOCTYPE html>
<html>
<head>
	<title>Loop Through an Indexed Arrays</title>
</head>

<body>

<?php
$books = array("HTML5", "CSS3", "JavaScript", "Bootstrap", "PHP");
$arrlength = count($books);

for($x = 0; $x < $arrlength; $x++) {
    echo $books[$x];
    echo "<br />";
}
?>

</body>
</html>
Output :
Associative array

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.

<!DOCTYPE html>
<html>
<head>
	<title>PHP Associative array</title>
</head>

<body>

<?php
$ages = array("Sri"=>56, "Ramana"=>27, "Roja"=>26);
print_r($ages); 
?>

<hr />
<p>Associative array <b>Looping</b></p>
<?php
$ages = array("Sri"=>56, "Ramana"=>27, "Roja"=>26);

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

</body>
</html>
Output :
Multidimensional array

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. An example of a multidimensional array will look something like this:

<!DOCTYPE html>
<html>
<head>
	<title>PHP Multidimensional array</title>
</head>

<body>

<?php
// Define nested array
$contacts = array(
    array(
        "name" => "Ramana Reddy",
        "email" => "ramana@freetimelearn.com",
    ),
    array(
        "name" => "Roja Reddy",
        "email" => "roja@freetimelearn.com",
    ),
    array(
        "name" => "Chanti",
        "email" => "chanti@freetimelearn.com",
    )
);
// Access nested value
echo "Ramana Reddy Email-id is: " . $contacts[0]["email"];
?>

</body>
</html>
Output :