Arrays

PHP Arrays are like the ultimate variable of PHP. It is also acts as a storage for a collection of elements. Arrays can contain other variables like strings, integers, or even other arrays in a list type format. Arrays are often used whenever you have an unknown amount of variables that you need to store for retrieval later. PHP array values can be outputted by iterating though them using a loop, or you can simply call a specific element by its index or key value. The key value of an array can be an implicit indexing system if you don’t provide the key. You can see more about the keys or indexes later in this tutorial. Arrays an asset to any programmer wanting more dynamic variables. Let’s look on how to construct an array in PHP.

Example

Result

17

We have our array $numberList with individuals elements of 1, 3, and 7. We reference the elements individually when we print them. The print_r($numberList[0]); prints out 1 because our element with an index of 0 in our array is 1. Since we didn’t provide a key value when we created the array, PHP allows us to retrieve the elements using a numerical index. Notice that we didn’t have to define our arrays upfront with PHP.

Example

Result

17

If you come from another language, you might take note that arrays in PHP are not fixed. Our new array is completely identical to the array in the previous example. This is just another way to add elements to our PHP array. I am just showing you this so I can open you up to the idea of dynamic adding elements to an array. So let’s use this new technique to create dynamic arrays.

The Dynamic Variable (Arrays)

Example

Result

Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 )

The way print_r prints an array is a little bit odd. All it does is tell you the index or key of the element and then the elements value. What you did here was dynamically create an array with values of 0-9 in that order. We created a for loop and each time we iterated, or went through the loop, we added a new key and value pair to our new array. Congratulations, you have learned a crucial lesson in the dynamic world of programming.

Common PHP Array Functions

I am going to run through some commonly used array functions.

array_pop() – removes the last element

Example

Result

Array ( [0] => a [1] => b )

array_push() – adds element to the end

Example

Result

Array ( [0] => a [1] => b [2] => c [3] => d )

array_search() – searches for value and returns its index

Example

Result

1

array_shift() – removes the first element and returns it

Example

Result

<?php aArray ( [0] => b [1] => c )

array_slice() – returns selected elements

Example

Result

Array ( [0] => b [1] => c )

array_splice() – replaces specified elements with new

Example

Result

Array ( [0] => a [1] => d [2] => e [3] => f )

array_unique() – removes duplicate values

Example

Result

Array ( [0] => a [1] => b [3] => c )

array_unshift() – adds element to the beginning

Example

Result

Array ( [0] => a [1] => b [2] => c [3] => d )

count() – counts all elements

Example

Result

3

sort() – sorts the array

Example

Result

Array ( [0] => a [1] => b [2] => c )

The functions above are some of the most commonly used array functions; however, you can find a complete list at PHP References. PHP arrays are an extremely powerful asset and are very useful when you are working with a few variables that deal with the same programming idea. They provide you with the functionality of not having to create a ton of single variables that pollute your code with a bunch of variables that are already used. They also give you the added benefit of not having to remember all of those variable names. Arrays can become complex when you get into arrays of arrays, but for now you should stay away from that headache.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Comment

  1. hi
    array_slice() example incorrect
    $eArray = array(“a”,”b”,”c”);
    print_r(array_slice($eArray,1,1));

    The third parameter should be 2

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.