Arrays, by Ben Mueller:

This tutorial will teach you how to utilize arrays in a PHP environment.


Creating an Array:

Arrays are a list of values or strings that can be used instead of declaring multiple variables. Creating an array in PHP is similar to creating an array in JavaScript. First declare a new variable using the $ symbol and then set that equal to a new array like so:

	$newarray = array("Burger", "Fries", "Onions", 1, 2, 3);

Of course the variable name newarray can be replaced with anything you want and you will need the array to be inside the PHP tags in order to get the array to do anything. I can combine strings and number values in one array like I did in this one if I want but it might get messy.


Calling Items:

What if I wanted to call a value from my array? PHP arrays are 0 based meaning that the first item in the array is actually the 0th. So counting the items in an array would be 0, 1, 2, 3 where 0 is the first, 1 is the second, and so on and so forth. So when I call an item from my array I have to take into account that the array is 0 based:

	$newarray = array("Burger", "Fries", "Onions", 1, 2, 3);
	
	echo $newarray[0];

Notice how we used the echo function, we'll go over this function later, for now just know that it prints the first item in my array to the screen.


Count function:

What if I wanted to know how long my array was? The function count() tells me how many items are in my array:

	$newarray = array("Burger", "Fries", "Onions", 1, 2, 3);
	
	echo count($newarray);

This should print 6 to the screen since there are six items in my array.


Printing your array:

Let's say you want to see your whole array and where the items in your array are placed. The print_r() function prints your array and the specific location of each item in order:

$newarray = array("Burger", "Fries", "Onions", 1, 2, 3);

print_r($newarray)

//returns Array("Burger" => 0, "Fries" => 1, "Onions" => 2, 1 => 3, 2 => 4, 3 => 5)

The => number indicated the items location in the array.

Changing Items:

If I wanted to go back later and change an item in my array I could do this:

$newarray = array("Burger", "Fries", "Onions", 1, 2, 3);
	
$newarray[0] = "Hot Dog";
	
echo $newarray[0];

This changes the item at position 0: Burger to the string Hot Dog. I can do this with any item in my array by changing the number within the brackets to the position of the item I want to change, and replacing it with a new value. One interesting thing about arrays in PHP is that you can use either curly brackets{} or normal brackets[] when calling an object in your array, so $newarray[0] could be $newarray{0}, this does not change what happens in the program at all.

Sorting Arrays:

Sorting an array can be extremely useful when wanting to display information in a certain way. There are many different functions associated with sorting that all do different(yet similar) things:

//sorts associative and multidimensional arrays or multiple arrays at the same time.
array_multisort($arrayname);

//sorts values in an array from low to high.
asort($arrayname);

//sorts values in an array from high to low.
arsort($arrayname);

//sorts keys in an array from high to low.
krsort($arrayname);

//sorts keys in an array from low to high.
ksort($arrayname);

//sorts values in an array naturally(lowest number to highest number), case insensitive.
natcasesort($arrayname);

//naturally sorts values in an array(case sensitive).
natsort($arrayname);

//sorts values from high to low.
rsort($arrayname);

//randomly sorts values.
shuffle($arrayname);

//sorts values from low to high(in my opinion this one is the easiest to remember)
sort($arrayname);

There are also several user defined sorting functions that you most likely won't need to know. If you do need to know about them feel free to do some research on the topic.


Removing Items:

What if I wanted to completely remove an item from my array? I could use the function unset() to delete any item of my choosing. If I wanted to get rid of all the numbers in my array I could do this:

$newarray = array("Burger", "Fries", "Onions", 1, 2, 3);
	
unset($newarray[3]);

unset($newarray[4]);
	
unset($newarray[5]);

This removes the items located at 3, 4 and 5 in the array so now my array is ("Burger", "Fries", "Onions", Undefined, Undefined, Undefined). Unset only removes the current value of that item in the array and does not completely delete that particular spot in the array.


Concatenating arrays:

You should remember concatenation from the last section, well guess what? You can concatenate Arrays as well! Just like when we echoed the first item in an array we can echo multiple items concatenated together:

$newarray = array("Burger", "Fries", "Onions", 1, 2, 3);
	
echo "I want a " . "$newarray[0] " . "with a side of " . "$newarray[1] " . "and some " . "$newarray[2] " . "on top ";

It's very difficult to see, but between the last letter of each string and the ending quotation marks is a space. If I add a dash everywhere there should be a space it would look like this: echo "I want a_" . "$newarray[0]_" etc... This is so the page prints "I want a Burger with a side of Fries and some Onions on top" instead of "I want aBurgerwith a side ofFriesand someOnionson top".


Associative Arrays:

Associative arrays are similar to normal arrays, but instead of just listing the values you set the values to a key like so:

$newarray = array("Main" => "Burger","Side" => "Fries","Topping" => "Onions");

Now if I want to call "Burger" from my array I can simply do this:

echo $newarray["Main"];

Associative arrays are slightly harder to write but allow you to acces items with personalized values, making it easier to sift through your array.


Multidimensional Arrays:

Multidimensional arrays are arrays that contain multiple arrays within them. A multidimensional array might look like this:

$newarray = array(array("Burger","Hot Dog"), array("Onions","Ketchup"), array("Fries", "Salad"));

As you can see the array $newarray contains three arrays that contain two different things each. What if I wanted to call something from this array? This time it gets a little more complicated:

$newarray = array(array("Burger","Hot Dog"), array("Onions","Ketchup"), array("Fries", "Salad"));
	
echo $newarray[2][0];

So how come I called two numbers? The first number [2] refers to the list of arrays, it calls the third array in the big array(remember 0 based!), and the [0] calls the first item in the third array. Thus when run, Fries should be printed on the screen. You can also create multidimensional associative arrays and concatenate multidimensional arrays for much more complex outcomes


Arrays are extremely good at storing information, and later, with the use of for loops and if then else statements they become extremely powerful tools. You can use them to store the number of visitors to your site, store emails input by users and much more.