ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.


Back to basics: PHP & Arrays

(Page 1 out of 3)


Lately the internet has been on a real fast track, and there have been many new developments, like Ajax, Web 2.0, Tags, and other interesting (and often hyped up) things. But this tutorial won't go there at all, and goes back to the beginning with the basics: using arrays in PHP.

If you're a seasoned PHP developer, who knows arrays like the back of his hand, this might be an article you'd want to skip. But if you're still unsure how arrays work, or if you're just curious about a few things, read on and learn more about arrays in PHP.

The Basics

Arrays can be seen as collections of variables. For example, let's say you want to store 10 colours, like so:

$color1 = 'blue';
$color2 = 'green';
$color3 = 'yellow';
// ... etc ...

This would be much more suited as an array, which would then look like this:

$color = array('blue', 'green', 'yellow');

Each color can then be used by appending a number to the color variable. For example, if you want to get 'green', you'd use the following variable:

echo $color['1']; // Outputs 'green'

It can be used just like any regular variable, and you can use it in any way you like. If you're wondering why I used '1' to get the second value, it's because the counter starts at zero. So the first item is item zero, the second item is item one, etc. Sounds confusing, but it becomes logical after a while.

You can also have "multi-dimensional" arrays, like so:

$people = array(
                array('Dennis', 'Pallett'),
                array('John', 'Doe'),
                array('Jane', 'Doe')

The people variable is an array with 3 items, and each of those 3 items are arrays again. To get the first name of the first person ('Dennis'), you'd use the following code:

echo $people['0']['0']; // Outputs 'Dennis'

You can nest arrays as deep as you want, and there's no limit to it. Be aware though that nesting arrays too deep can cause problems, because it'll make your code harder to read. That's why I don't recommend nesting 16 arrays or 15, or even 10.

One way to make your arrays is bit easier to understand is to use associative arrays, which can have textual keys instead of numbers. The best way to understand this is with a quick example:

$people = array(
                array('first' => 'Dennis', 'last' => 'Pallett'),
                array('first' => 'John', 'last' => 'Doe'),
                array('first' => 'Jane', 'last' => 'Doe')

Again we have the people array, but this time each item is an associative array, and textual keys are used. To get the first name of the first person now, it requires the following code:

echo $people['0']['first']; // Outputs Dennis

This is much easier to understand, and even if you know nothing about the original array, you can probably deduce that it's showing the first name of a person. Like normal arrays, you can nest as many arrays as you want, and you can also mix normal arrays with associative arrays, e.g.

$arr = array('one' => '1', '2', 'three' => '3');

The above array has two items with a textual key, and one item with a numerical key. To access the second item, you have to use the following code:

echo $arr['0']; // Outputs 2

Even though it's the second item, its key is '0' because it's the first item without an associative key.

In all the above examples, we've always created an array using the array() function, and including some values. But what if you've already got an array and what to add another value? That's no problem, as there are several ways to add a new value:

$myarr = array('one', 'two');

// Different ways to add a new item:
$myarr[] = 'three';
$myarr['somekey'] = 'four'; // with a key
array_push ($myarr, 'five');

You can also edit an item, simply by setting a new value, like so:

$myarr = array('one', 'mykey' => 'two');

// Edit both items (one without key and one with)
$myarr['0'] = 'three';
$myarr['mykey'] = 'four';

Arrays are one of the most important things of PHP (or any other language for that matter) and when you're creating scripts with PHP you'll probably be using them 90% of the time, especially when you're dealing with a database. You can do so much with arrays, and the above is just the basics. Let's have a look at some commonly used array functions now.

Next: Commonly Used Functions »

5 Responses to “Back to basics: PHP & Arrays”

  1. OAKA Blog » Blog Archive » Using Arrays in PHP Says:

    […] Site: […]

  2. CubSpeaking » Using Arrays in PHP Says:

    […] Site: […]

  3. Bocse L Filip Says:

    The natural algorythm is not only the human-like ordering algo. If a machine would see strings the way we do the machines would also naturally sort in the same manner as humans…. only difference is the representation of a string.
    Of course we see strings as a set of various characters different by shape, meaning and so on… not the machines though… these only see 1’s and 0’s .It is true these beautifull machines have the ability to represent these 1’s and 0’s in a human understandable representation but it is just like a translation from their language (1’s and 0’s ) to a language which they do not understand… based on a strict dictionary.
    It is sad :) but true… the machines have no imagination. May one teach a machine what imagination is it is very likely our natural algo will seem redundant to whatever these machines may consider “natural algo”.

  4. Secret Black Book of IceFuzion » Back to basics: PHP & Arrays Says:

    […] read more | digg story […]

  5. Ocus Says:

    Playing The Angel

    PHPit - Totally PHP &r…

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Basics
  2. Commonly Used Functions
  3. Sorting Functions
Bookmark Article
Download Article
Download this article as a PDF file