Associative Arrays

Contact Us or call 1-877-932-8228
Associative Arrays

Associative Arrays

Whereas indexed arrays are indexed numerically, associative arrays are indexed using names. For example, instead of Ringo being indexed as 3, he could be indexed as "drummer".

Initializing Associative Arrays

Like with indexed arrays, we can intialize a zero-length associative array and then add elements.

Syntax

$beatles = array();
$beatles['singer1'] = 'Paul';
$beatles['singer2'] = 'John';
$beatles['guitarist'] = 'George';
$beatles['drummer'] = 'Ringo';

Or the array could be created in a single line as follows.

Syntax

$beatles = array('singer1' => 'John',
											'singer2' => 'Paul',
											'guitarist' => 'George',
											'drummer' => 'Ringo');

Reading from Associative Arrays

Reading from associative arrays is as simple as reading from indexed arrays.

Syntax

echo $beatles['drummer']; //outputs Ringo to the browser

Looping through Associative Arrays

The following code will loop through the entire $beatles array outputting each element and its key to the browser.

Syntax

foreach ($beatles as $key => $beatle)
{
	echo "<b>$key:</b> $beatle<br>";
}

The above code snippets are combined in the following example.

Code Sample:

Arrays/Demos/AssociativeArrays.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Associative Arrays</title>
</head>

<body>
<h1>Associative Arrays</h1>
<?php
	$beatles = array('singer1' => 'John',
					'singer2' => 'Paul',
					'guitarist' => 'George',
					'drummer' => 'Ringo');

	echo $beatles['drummer']; //outputs Ringo to the browser
?>
<hr>
<?php
	foreach ($beatles as $key => $beatle)
	{
		echo "<b>$key:</b> $beatle<br>";
	}
?>
</body>
</html>

Superglobal Arrays

The superglobal arrays are associative arrays. The file below outputs all the contents of the superglobal arrays using foreach loops.

Code Sample:

Arrays/Demos/SuperGlobals.php
<?php
	session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Superglobal Arrays</title>
</head>

<body>
<h1>Superglobal Arrays</h1>
<h2>$_COOKIE</h2>
<ol>
<?php
	foreach ($_COOKIE as $key => $item)
	{
		echo "<li><b>$key:</b> $item<br></li>";
	}
?>
</ol>
<hr>
<h2>$_ENV</h2>
<ol>
<?php
	foreach ($_ENV as $key => $item)
	{
		echo "<li><b>$key:</b> $item<br></li>";
	}
?>
</ol>
<hr>
<h2>$_FILES</h2>
<ol>
<?php
	foreach ($_FILES as $key => $item)
	{
		echo "<li><b>$key:</b> $item<br></li>";
	}
?>
</ol>
<hr>
<h2>$_GET</h2>
<ol>
<?php
	foreach ($_GET as $key => $item)
	{
		echo "<li><b>$key:</b> $item<br></li>";
	}
?>
</ol>
<hr>
<h2>$_POST</h2>
<ol>
<?php
	foreach ($_POST as $key => $item)
	{
		echo "<li><b>$key:</b> $item<br></li>";
	}
?>
</ol>
<hr>
<h2>$_REQUEST</h2>
<ol>
<?php
	foreach ($_REQUEST as $key => $item)
	{
		echo "<li><b>$key:</b> $item<br></li>";
	}
?>
</ol>
<hr>
<h2>$_SESSION</h2>
<ol>
<?php
	foreach ($_SESSION as $key => $item)
	{
		echo "<li><b>$key:</b> $item<br></li>";
	}
?>
</ol>
<hr>
<h2>$_SERVER</h2>
<ol>
<?php
	foreach ($_SERVER as $key => $item)
	{
		echo "<li><b>$key:</b> $item<br></li>";
	}
?>
</ol>
</body>
</html>

Don't worry about the session_start() statement at the top. We'll cover that in detail later in the course.

Next