Published: 19th Dec 2017 | Words: 1,292 | Reading Time: 7 minutes


Understanding PHP arrays

In PHP arrays are one of the most powerful tools you have for working with different types of data across different areas of your application. An array is basically a holder for multiple types of data and values. Instead of assigning a single value to a variable you can assign multiple values to an array and assign that array to a variable. What this means is you can group multiple values together in a variable and reference what you need when you need. If you start to do a lot of programming you will quickly see that arrays are incredibly powerful tools and you can use things like loops to output complex arrays with multiple values of different data types.

Numeric arrays

Numeric arrays are very easy to understand. A numeric array containing different values can be referenced by the position of the values you want to find. Like most things in programming, the index of a numeric arrays starts at 0 and works its way up, so if you have an array with 10 different values within it you can reference the first value by using 0 and reference the last value by using 9. Numeric arrays are usually best suited to very basic groupings of data.

$array = array("one", "two", "three"); // declaring an array with 3 values

echo $array[0]; // outputs one
echo $array[1]; // outputs two
echo $array[2]; // outputs three

While the array is quite simple itself it’s the range of array functions in PHP that make them so powerful. You can remove and add array elements. You can perform specific functions on each array element using things like array_walk. You can order the array by using the list function. Because of this power you can take large groups of data, put it into arrays and sort it or filter it however you would like. There is no end to what can be done and you can take data that would take hours or days to filter and perform those actions in seconds or minutes using arrays.

Associative arrays

At first associative arrays can seem quite hard to get your head around but a very simple explanation of them is this. Instead of having numeric indexes e.g. the first array element is 0, the second is 1 etc. you can have words as the references of the array values. What this means to you is that instead of having complicated arrays with confusing numbers as the references you can assign words as the array keys and in turn make navigating and referencing your associative arrays very easy. You will find that generally most arrays will be referenced associative keys simply because it’s harder to keep track of numbers than it is a descriptive piece of text.

$array = array(
   "one" => "first value"
echo $array['one'] // outputs "first value"

GET URL parameters are built on associative arrays and you will probably hear the words “key” and “value” a lot when you first start working with URL parameters. Passing key value pairs in URL’s can help with a lot of different things like generating dynamic pages and passing values to be used in certain way on a page or in a database although there are numerous security concerns when you use URL parameters. // a key value pair in a URL parameter

echo $_GET['key'] // outputs “value” because you are referencing the key name but outputting the value // another key value pair in a URL parameter

echo $_GET['name'] // outputs "andy"

Multidimensional arrays

Multidimensional arrays are arrays containing arrays and are an even more powerful way of grouping values together and streamlining how your build your application. Multidimensional arrays can become extremely complex as you can have array within array within array within array. As you build more complex applications you will inevitably find yourself needing to build more complex arrays and once you understand the basic numeric and associative array structure it will not take you long to figure out how to build multi-dimensional arrays and open up yourself to the endless possibilities they bring.

$array = array(
      "one" => "first array – first value",
      "two" => "first array – second value"
      "one" => "second array – first value",
      "two" => "second array – second value"

echo $array[0] // outputs error as you are trying to echo an array element that is an array.

echo $array[0]['one'] // outputs "first array – first value" as you reference both array positions

echo $array[1]['two'] // outputs "second array – second value"

The above example should hopefully be easy for you to understand. If you try to output $array[0] an error occurs. This is because you are reference an array and to output an array you usually have to loop through each array element. For the purpose of this exercise we are trying to reference single array elements only and to reference either of the two elements in each array we must reference the array first - $array – then the array underneath – [0] or [1] – and finally the element within either array so [‘one’] or [‘two’]. You can make arrays as deep as you want with array within array within array but it would probably get extremely hard to keep track of arrays that are too deep. Realistically you will end up with arrays two or three levels deep usually and when you consider how much information you can actually store in multidimensional array you can use them to group information and use either all or some of it in different parts of your scripts.