Arrays in PHP - contain different and even mixed data types
Archive - Originally posted on "The Horse's Mouth" - 2013-04-24 18:11:38 - Graham EllisPHP's arrays can be keyed by both value and by string... and on this week's PHP course we've covered both. An indexed array (not really an array - rather akin to a list in Perl or Python) starts at index element number 0 and works up from there. Each array member may contain a different type of data, and the members may be of different sizes. And they can be set up initially with an array function call, or by return from any number of functions.
A mixture of ints and floats:
$speeds = array(0,1,5.5,11,19,28,38,49,61,74,88,102,117);
Strings of different length:
$windy = array("Calm","Light Air","Light Breeze","Gentle Breeze" ...
The resultant parts from an explode function:
$current = explode("\t",$station);
The rows of data returned by a web service:
$stations = file("http://www.wellho.net/data/railstats.xyz");
Once an array has been set up, you can even add other values onto the end of it ...
Source code example, together with a demonstration of looping through an array to find a threshhold value (conversion of wind speed to beaufort scale) from the course - [here].