Tutorials

Learn web design and programming with our free video and text tutorials.

Web Designer? Design and market your own professional website with easy-to-use tools.

PHP Associative Arrays Tutorial

PHP associative arrays allow you to access the values in the array through the use of named string ID keys for each stored value.

There are 2 different ways of creating arrays.

Example 1:

<?php
$salaries = array("Angel"=>2000, "Billy"=>3000, "Charles"=>4000);

echo "Angle makes - $" . $salaries["Angel"] . " a month.<br />";
echo "Billy makes - $" . $salaries["Billy"] . " a month.<br />";
echo "Charles makes - $" . $salaries["Charles"] . " a month.";
?>

The code above outputs:

Angle makes - $2000 a month.
Billy makes - $3000 a month.
Charles makes - $4000 a month.

Example 2:

<?php
$salaries['Angel'] = "2000";
$salaries['Billy'] = "3000";
$salaries['Charles'] = "4000";

echo "Angle makes - $" . $salaries["Angel"] . " a month.<br />";
echo "Billy makes - $" . $salaries["Billy"] . " a month.<br />";
echo "Charles makes - $" . $salaries["Charles"] . " a month.";
?>

The code above outputs:

Angle makes - $2000 a month.
Billy makes - $3000 a month.
Charles makes - $4000 a month.