Home /PHP/Arrays

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

$numberList =  array(1,3,7); //Our new array
print_r($numberList[0]); //this prints 1
print_r($numberList[2]); //this prints 7

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

$numberList2 = array(); //Notice our array is empty
$numberList2[0] = 1; //We set the first element equal to 1
$numberList2[1] = 3; //We set the second element equal to 3
$numberList2[2] = 7; //We set the third element equal to 7
print_r($numberList2[0]);
print_r($numberList2[2]);

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

$numberList3 = array();
for($i = 0; $i < 10; $i++)
{
    $numberList3[$i] = $i;
}
print_r($numberList3);

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

$aArray = array("a","b","c");
array_pop($aArray);
print_r($aArray);
Array ( [0] => a [1] => b )

array_push() – adds element to the end

$bArray = array("a","b","c");
array_push($bArray,"d");
print_r($bArray);
Array ( [0] => a [1] => b [2] => c [3] => d )

array_search() – searches for value and returns its index

$cArray = array("a","b","c");
echo array_search("b",$cArray);
1

array_shift() – removes the first element and returns it

$dArray = array("a","b","c");
echo array_shift($dArray);
print_r($dArray);
aArray ( [0] => b [1] => c )

array_slice() – returns selected elements

$eArray = array("a","b","c");
print_r(array_slice($eArray,1,1));
Array ( [0] => b [1] => c )

array_splice() – replaces specified elements with new

$fArray = array("a","b","c");
$gArray = array("d","e","f");
array_splice($fArray,1,2,$gArray);
print_r($fArray);
Array ( [0] => a [1] => d [2] => e [3] => f )

array_unique() – removes duplicate values

$hArray = array("a","b","b","c","c","c");
print_r(array_unique($hArray));
Array ( [0] => a [1] => b [3] => c )

array_unshift() – adds element to the beginning

$iArray = array("b","c","d",);
array_unshift($iArray,"a");
print_r($iArray);
Array ( [0] => a [1] => b [2] => c [3] => d )

count() – counts all elements

$jArray = array("a","b","c");
echo count($jArray);
3

sort() – sorts the array

$kArray = array("c","b","a",);
sort($kArray);
print_r($kArray);
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.