FreeKB - PHP Getting Started with key value pairs
PHP - Getting Started with key value pairs

Let's say you want to create the following key:value pairs.

Key Value
Name Jeremy
ID 123456
Occupation Engineer

 

Arrays are used to create key value pairs.

$array = array(
    "name" => "Jeremy",
    "id" => "123456",
    "occupation" => "engineer"
);

 

 

var_dump can be used to print the data structure of the array.

var_dump($fruit);

 

Which will return the following.

array(3) { [name]=> string(6) "Jeremy" [id]=> string(6) "123456" [occupation]=> string(8) "engineer" }

 

Specific elements can be printed, like this.

echo "array[name]       = " . $array[name] . "<br />";
echo "array[id]         = " . $array[id] . "<br />";
echo "array[occupation] = " . $array[occupation] . "<br />";

 

Which will return the following.

array[name] = Jeremy
array[id] = 123456
array[occupation] = engineer

 

Or you can loop through the array.

foreach ($array as $value) {
  echo "$value<br \>";
}

 

Which will return the following.

Jeremy
123456
engineer

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 114d0 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |