Array functions in PHP
Arrays, Arrays, Arrays, always it’s Arrays!
To help your life getting a little bit easier, I would like to give you some pointers on how to work with Arrays. There are maybe 5 functions that are more important than the other array functions, so read this introduction and get a head start with Arrays in PHP
What does an array look like?
An array is pretty much a way for a programming language to hold data from a table. An array can be one, two, three (and more) dimensional. An one dimensional array is pretty much only a list, with words/numbers one after the other. Two dimensional (the one you will work with mainly) is more useful and is made of something that look very much like an excel spreadsheet. Here’s an example of an associative two dimensional array:
firstname => "Jake" lastname => "Svensson" age => 29
The data above is a great example of a two dimensional array. The strings on the left side is called keys and the things on the right side is called values.
To create an array like this you write in PHP:
$myArray = Array("firstname" => "Jake",
"lastname" => "Svensson",
"age" => 29);
If you want to write out the lastname for example you use this PHP code:
echo $myArray["lastname"];
Pretty simple right
Using foreach on Arrays
foreach is a great function that most people miss in the beginning when coding PHP. That’s really stupid because it’s a useful function for handling with Arrays. Look at this piece of code and I will explain what it does after the code:
foreach($myArray as $key => $value)
{
echo $key . "-". $value . "
";
}
This little neat piece of code will output the keys with the associated value. So the output will be something like this:
firstname – Jake
lastname – Svensson
age – 29
array_values and array_keys
Depending on what kind of arrays you work with, sometimes you need to get only the keys or only the values to work with. These two functions returns only the values or keys
You will get a new array with the result, and it will be as a none-associative array with numbers as index instead.
array_unique
I don’t use this function that often, but when I need it I love it! Pretty simple, but really effective function that removes all duplicate values from an array. Important to remember is that it removes duplicate values, not keys (because you can’t have duplicate keys). I have used this function some times when I parse a text file with data that I am not sure is 100% correctly formatted. It’s a good way to remove unwanted duplicate strings. You can use this function on both associative and none associative arrays!
size (or count)
size is only an alias for count, but I use the function size anyway. Don’t ask my why, just do it ![]()
size($myArray) pretty much returns the size of the array. and in this case we get the value 3 back from the function. It is very often used when dealing with arrays. If you use a for-loop you write for($i = 0; $i < sizeof($myArray); $i++). If you want to give out some simple information to your visitors you will most probably write something like this:
echo "The array has got " . sizeof($myArray) . " values";
That was all for todays lesson, come back soon again for part two of this tutorial.
If you want some start help on String functions in PHP read my past tutorial also about that.

Leave a Reply