Submitted by saagar.jain on Sat, 2007-05-19 15:17.
In an associative array a key is associated with a value. If you wanted to store the salaries of your employees in an
array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys
in our associative array, and the value would be their respective salary.
PHP Code:
$salaries["Bob"] = 2000;
$salaries["Sally"] = 4000;
$salaries["Charlie"] = 600;
$salaries["Clare"] = 0;
echo "Bob is being paid - $" . $salaries["Bob"] . "";
echo "Sally is being paid - $" . $salaries["Sally"] . "";
echo "Charlie is being paid - $" . $salaries["Charlie"] . "";
echo "Clare is being paid - $" . $salaries["Clare"];
Re: How to make associative arrays in Php ?
In an associative array a key is associated with a value. If you wanted to store the salaries of your employees in an
array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys
in our associative array, and the value would be their respective salary.
PHP Code:
$salaries["Bob"] = 2000;
$salaries["Sally"] = 4000;
$salaries["Charlie"] = 600;
$salaries["Clare"] = 0;
echo "Bob is being paid - $" . $salaries["Bob"] . "";
echo "Sally is being paid - $" . $salaries["Sally"] . "";
echo "Charlie is being paid - $" . $salaries["Charlie"] . "";
echo "Clare is being paid - $" . $salaries["Clare"];