Skip to content

Instantly share code, notes, and snippets.

@PeeHaa
Created August 31, 2012 13:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeeHaa/3552723 to your computer and use it in GitHub Desktop.
Save PeeHaa/3552723 to your computer and use it in GitHub Desktop.
If I see one more question about arrays in PHP...

Arrays can be seen as lists which can hold information. Arrays in PHP can holds all types of values like scalar values, objects or even other arrays.

Declaring an initializing arrays

First lets see how we can declare an empty array:

<?php
$theArray = array();

Once we have an array we can add items to our array:

$theArray[] = 'First item in the array';
$theArray[] = 'Second item in the array';

If you add items to our array as shown above you have created an array as following:

array(2) {
  [0]=>
  string(23) "First item in the array"
  [1]=>
  string(24) "Second item in the array"
}

As you can see the array now contains 2 items. Another thing you should note is that the items have "labels". The first item in the array is 0 and the second item is 1. This is because we have added the items using the $theArray[] syntax. This automatically creates a new "label" of key for the new item you are adding to the array. This label or key is also known as the index. As you should see by now in PHP array indices start with 0.

Another way of defining an array is by using named keys (so called associative arrays). This can be useful, because a name can say so much more about what the item contains than just a numeric value. To define such an array you can do something like the following:

<?php
$user = array();
$user['name'] = 'PeeHaa';
$user['age'] = 28;

The above array will result in:

array(2) {
  ["name"]=>
  string(6) "PeeHaa"
  ["age"]=>
  int(28)
}

As you can see the keys in our array are now named rather than just numeric.

It is also possible to initialize the array values when declaring a new array. If you for example take the previous example of the $user array:

<?php
$user = array(
    'name' => 'PeeHaa',
    'age'  => 28,
);

This would result in the exact same array as in the previous example. After you have initialized you can still add new items to the array as seen previously:

$user['length'] = 1.8;
$user['country'] = 'nl';

If you now look at the contents of the $user array it would contain:

array(4) {
  ["name"]=>
  string(6) "PeeHaa"
  ["age"]=>
  int(28)
  ["length"]=>
  float(1.8)
  ["country"]=>
  string(2) "nl"
}

You can do the exact same thing for arrays with numeric keys (i.e. initializing when declaring it):

<?php
$theArray = array(
    'First item in the array',
    'Second item in the array',
);

You should note that the last comma (,) in the initialization of the array is not strictly necessary, but once you are going to work with some versioning system it is better to do it, because if you would add another item to the array in the initialization your versioning system will note that there are two lines changed instead of one (because you would have to add the comma).

As already stated it is also possible to add arrays to arrays. These are so-called multidimensional arrays and can be declared as follows:

$users = array(
    array(
        'name' => 'PeeHaa',
        'age'  => 28,
    ),
    array(
        'name' => 'SomeRandomPerson',
        'age'  => 42,
    ),
);

In this example you have created an array with 2 levels. The first level contains 2 persons. The second level contains the information about the person. This array looks like:

array(2) {
  [0]=>
  array(2) {
    ["name"]=>
    string(6) "PeeHaa"
    ["age"]=>
    int(28)
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(16) "SomeRandomPerson"
    ["age"]=>
    int(42)
  }
}

Accessing array items

Now that you have seen how you can declare and initialize arrays in PHP lets see how you can access individual array items.

New array syntax

Note: As of PHP 5.4 the shorthand syntax is supported for arrays meaning we can do things like:

// Declare an empty array
$theArray = [];

// Declare and initialize an array
$theArray = [
    'key' => value,
    'somearray' => [
        'key' => 'value',
    ],
];

The only thing that is different is the syntax. To access / mutate or do whatever with the array is exactly the same as described before with the pre 5.4 syntax.

Todo

splfixedarray "huge" size of array in PHP concatenating arrays looping through arrays

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment