Friday 29 June 2012

All about PHP arrays

Why write this ? Because I'm a web developer who never stores anything in his head, especially since google made searching the web even easier.

Anyway like I said, i'm a web developer at Computer Related (www.computerrelated.co.uk), in the UK. Well in Birmingham, West midlands to be precise.


I have many more tutorials and self help pages over at teachme.webege.com.

The basics

What is an array? I could get all technical up in here, but why! Really all you need to know is that an array stores multiple pieces of data in one variable.

OK maybe I need to be a bit more technical ;-) I find examples work best

Imagine the sum below

3 + 3 = 6
Easy. But long winded to change.

Now imagine this

x + y = z
$x = 3, $y = 3 and $z = both x plus y
Now we are working with variables. We can make x and y anything and z will always add up. Cool.

So what has this got to do with arrays? Everything. An array is made up of multiple variables. See.

For the next example I will create an array and add some data.

$array = array(4, 5, 8)
Now we have
$array[0] = 4, $array[1] = 5, $array[2] = 8

Do you understand what just happened? the variable $array was given 3 pieces of data 4, 5 and 8. The data is automatically stored in indexed order i.e 1, 2, 3, 4, etc.

So to access the data i just need to use $array[index], index being the actual index number.

Hold on, so why does it start from 0

Well spotted, we naturally count from 1 to 10 discounting 0, but computers count from 0 to 9. Hence 0 being the first index.

So what can I use an array for really

Well primarily instead of having for instances of 5 variables you could have one array, see below for example.

$one, $two, $three, $four, $five
or
$numbers[index]

We can also do cool stuff like use them in loops, but since I haven't discussed loops yet i'll leave that for now.

Back to arrays, lets say you want declare an empty array so you can add data later

$foo = array()
Now you can add to it like this
$foo[] = 23
That will add data to next free slot in array.

I don't want to use numbers for my index, i'd prefer to use words. OK, that can be done. It's called an associative array

$array = array("one" => 1, "two" => 2, "three" => 3, "four" => 4)
Now
$array['one'] = 1, and $array['two'] = 2, etc.

Suppose i have an associative array and I want it to be indexed instead? Of course we got a solution for that too.

$new_array = array_values($array)

Oh my days, I know that's what your thinking, yes it's that easy.

OK, how do i shuffle the array? If your not in PHP then long ting, but in PHP it's as quick as a quick thing lol.

$new_array = shuffle($array)

Done. I know, this PHP stuff is easy. Be carefull though, shuffle changes the index completely, wiping over old indexes.

Ok, you got this far so i'm going to give you 2 extra tips, not directly array related but near enough.

explode() is used to seperate a string at specific points (known as delimeters), i.e comma, fullstop, space, etc. Once exploded the data is then stored in an array.

$string = "hello, clarent, from, www.computerrelated.co.uk"
$array = explode(',', $string)

Now we have
$array[0] = hello, $array[1] = clarent, $array[3] = from, etc

Another cool tip is - Every string variable is an array. So

$string = "hello", then $string[0] = "h", $string[1] = "e", etc

If i've missed any usefull array stuff out please let me know so we can all grow.