If you learn like I do, then it will probably be best if I show you some code before I move on. Creating an array with PHP is pretty simple.
Here are a few examples of how to create an array with PHP. Read the comments for the explanation.
PHP Code:
// empty array
$fruit = array();
// create an array of fruit
$fruit = array('apple', 'orange', 'pear');
// add a pineapple to the fruit array
$fruit[] = 'pineapple';
// now there are 4 items in the fruit array
That is nice, but when would I ever create a fruit array? I know it's not a very practical example so I'll attempt to go further. If you have never used a mysql database before, just bare with me.
This example shows how one might get all the products from a database and return an array.
PHP Code:
function getProducts(){
// initialize empty array
$products = array();
// query the database
$result = mysql_query("select product_name from products");
// loop through results
while ($row = mysql_fetch_assoc($result)) {
// append product array
$products[] = $row['product_name'];
}
return $products;
}
Now this is not a very thorough example because of course you would most likely be dealing with other data such as price, category, etc with real store data, but it is a little more real-world than the fruit sample.