My functioning Array script can be viewed here:
Basic Arrays
For now, we're going to stick with a basic array.
But wait!.. What is an array?
An array is a way of storing mutliple values in a single variable.
There are two main types of arrays:
Numeric arrays
Associative arrays
Numeric Arrays
If we wanted to make a list of foods, we could use
$food1 = "Apple";
$food2 = "Banana";
$food3 = "Orange";
or
We can use a numeric array, and this is how it would look.:
$food[0] = "Apple";
$food[1] = "Banana";
$food[2] = "Orange";
The difference is our numeric array holds all three food values in one variable, $food. And to echo these is the same as echo'ing a regular variable.
<?
$food[0] = "Apples";
$food[1] = "Bananas";
$food[2] = "Oranges";
echo $food[0] . " and " . $food[1] . " and " . $food[2] .", oh my!";?>
Will display:
Apples and Bananas and Oranges, oh my!
In $food you see the hard brackets, with a digit inside. That digit is called a key, and will always start at 0 unless you tell it otherwise.if you are completely new to programming, this might seem wierd, but this is typically how computer languages count - starting from zero.if you don't like this method, you can always change the key to a string, rather than an integer.if the key is a string, it's not a numeric array anymore, it's called an associative array.
Associatve Arrays
Sticking with our food examples, let's take a look at a different way we can store our list of fruits.
$food["Apple"] = "Green or red";
$food["Banana"] = "Green or yellow";
$food["Orange"] = "Never green, always orange";
You can echo the variables the same way as a numeric array: <?=$food["Apple"];?>
if you're what the flip I just wrote - that's the shorthand way to echo something. <?=$my_variable." with some extra text";?>
Using Arrays
So up until now, you may have asked yourself, "Why, or when will I ever use these?". Good question. You can use an array instead of a switch statement. We do this by using the in_array() function. Let's look at an example:
<?
$food = array("Apple", "Banana", "Orange"); // yes, we can write arrays a short way as well :)
if(in_array("Pear", $food))
{
echo "There is a pear in my array";
}
else
{
echo "Nope, no pear in here!<br />";
echo "We only have a:<br />";
foreach($food as $item)
{
echo $item . "<br />";
}
}?>
Displays:
Nope, no pear in here!
We only have:
Apple
Banana
Orange
This might look big at first, but read through it line for line...
Now i'll explain every part.
- We created the array called $food
-
We used the in_array() function. How this works:
in_array($needle, $haystack). PHP will look for the needle in a haystack. Our code is looking for "Pear" in a list of foods that had Apple, Banana, and Orange in it.
if PHP can find the $needle in the $haystack, it returns true. Which is why our if statement failed, and went to the else statement. (Because there is no "Pear") - The in_array() returned false, so it went to the else statement, which said "Nope, no pear in here! We only have a:"... That as far as we know.
-
the foreach() function is one of the many ways to list off each value in an array. It works like this:
foreach($array as $new_variable). the "as" in the middle assigns $new_variable the value of the current $array value. Now you can use $new_variable inside the curly brackets as the listed item. It will go in numerical order, which is why it's important to know numerical arrays. - We then echo'd each food in the array followed by a <br />.
Now read that example one more time, line for line. Yes, that can be painful, but it will help you with this.
Ok, you've read it at least two times, probably more.. It's not too difficult is it? We got rid of the switch statement, and made our first loop
Once we get a little farther into this course, I'll teach you guys about multidimensional arrays, how to add to an existing array, and a few more useful array functions.
Our next few lessons will be about loops. This is where it starts to get a little bit more fun and dynamic.
Your task:
Create an array, any way you like..
echo two values from that array.
Then create a foreach loop to echo all values of that array.