Breadcrumbs

PHP Arrays

Arrays are useful as they allow you to store multiple values under one name rather than having multiple variables with multiple names for storing a list of related values. An array is a sort of ordered map as you have a key that points you to the value of that key. You will see this as you continue learning PHP Arrays.

The first step to creating an array is to declare one. Declaring an array uses the array() function:

Code: PHP

<?php
$name 
= array("One" => "book"=> "dude");
?>

This array contains two values and we can access the data using the key value that we assigned the data. Here is the format of an array for better understanding.

Code: PHP

<?php
$array_name 
= array(key => "value"key => "value");
?>

So the keys in the array that we did above would be One and 2. Note that assigning words for keys isn't always the best thing to do and using numbers is easier when coding, but I used characters here to show that it is possible. Now we can access the data in an array using this format:

Code: PHP

<?php
$array_name
[key];
?>

And here is how we could use it to access one of the values from our array:

Code: PHP

<?php
$name
[2];
?>

This would return 'dude' but as we don't do anything with it we don't get any output. But you can output the array value to the screen pretty easily:

Code: PHP

<?php
echo $name[2];
?>

You can also assign the value to variables and work with it from there. Now one thing you will eventually do with an array is loop through all the values that the array holds. You can do this with loops. I won't get into this as I have covered it in the last PHP tutorial I published on PHP Loops. But you can also easily print all the values of an array using something like:

Code: PHP

<?php
print_r
($name);
?>

The looping methods are when you want to do something else with the data in the array, such as editing it using regular expressions and printing other text with it.

Deleting Values From an Array

To delete an array or a particular value from an array you use the unset() function built into PHP. As the parameters of the unset() function you place the array value/name that you wish to delete:

Code: PHP

<?php
unset($name[2]); // This removes the element from the array
unset($name); // This deletes the whole array
?>

Naming Keys

When naming a key for an array value and you use characters other than numbers make sure to place the characters in single quotes (''). If you don't place the quotes it will still work, but it isn't right. This is because without the quotes it is an undefined constant, but PHP places the name of the constant there as its value although it was never declared.

Array Functions

Array have a lot of functions and there are so many it is hard to go over even basic ones. Although the PHP guide on the official PHP site, php.net, has comprehensive guides for each function (external link). There are many useful array functions such as adding all the array values, sorting array values, removing duplicate entries, and more. Arrays provide easy data manipulation for a group of data, and this manipulation is provided with the numerous array functions.

Summary

Now as you know the fundamentals of PHP Arrays you can move on to using the array functions and building array use into your applications.

Tags: