Saturday, December 8, 2012

PHP Arrays - PHP Arrays For Beginners Lesson 7

ARRAYS
By Stephon Unomon

 

          An Array can be thought of as a single variable that stores more than one value. An array uses a key to determine what value to reference. So;

 

$array[key] = value;

 

Key values start at “0” normally, as PHP likes to number things starting at Zero instead of One. It’s a programming thing, I don’t know either.

 

Let’s use our truck examples from above, and assign then in an array.

 

<?php

$truck_array[0] = "Toyota";

$truck_array[1] = "Dodge";

$truck_array[2] = "Chevy";

$truck_array[3] = "Ford";

?>

 

And here’s how we could output information from the array:

 

<?php

echo "Two great truck makers are "

. $truck_array[0] . " & " . $truck_array[1];

echo "<br />Two more great truck makers are "

. $truck_array[2] . " & " . $truck_array[3];

?>

 

Here’s is the output result of the above array:

 


 

 

 Associative Arrays

 

       An Associative Array is an array in which the keys are associated with values.

 

<?php

$truck[“Toyota”] = Tundra;

$truck[“Nissan”] = Titan;

$truck[“Dodge”] = Ram;

?>

 

A Syntax example using the Associative Array above:

 

echo "Toyota makes the " . $truck[“Toyota”] . "<br />";

echo "Nissan makes the " . $truck[“Nissan”] . "<br />";

echo "Dodge makes the " . $truck[“Dodge”];

 

And, when viewed in a browser...

 

You may not see the usefulness of the Array and Associative Array right now, but I think (hope) it will come together a little more once we hit the next lesson - LOOPS.

 
Kool PHP Suite

No comments:

Post a Comment