Categories
Uncategorized

Using an associative array to replace text in PHP

Imagine you have to write out some attribute names from your database, but want to replace them with more user-friendly versions. For instance, "First Name: " rather than "fname".

An assocative array in PHP will do the trick nicely.

For instance:

1
2
3
// labels array will be used to replace the database attribute names with field labels
$labels = array("lname"=>"Last Name","fname"=>"First Name");
echo "This line displays the ".$labels["lname"];

returns

This line displays the Last Name

Leave a Reply