Книга: Fedora™ Unleashed, 2008 edition

Arrays

Arrays

Arrays are one of our favorite parts of PHP because the syntax is smart and easy to read and yet manages to be as powerful as you could want. You need to know four pieces of jargon to understand arrays:

? An array is made up of many elements.

? Each element has a key that defines its place in the array. An array can have only one element with a given key.

? Each element also has a value, which is the data associated with the key.

? Each array has a cursor, which points to the current key.

The first three are used regularly; the last one less so. The array cursor is covered later in this chapter in the "Basic Functions" section, but we look at the other three now. With PHP, your keys can be virtually anything: integers, strings, objects, or other arrays. You can even mix and match the keys so that one key is an array, another is a string, and so on. The one exception to all this is floating-point numbers: You cannot use floating-point numbers as keys in your arrays.

There are two ways of adding values to an array: with the [] operator, which is unique to arrays, and with the array() pseudo-function. You should use [] when you want to add items to an existing array and use array() to create a new array.

To sum all this up in code, Listing 27.2 shows a script that creates an array without specifying keys, adds various items to it both without keys and with keys of varying types, does a bit of printing, and then clears the array.

LISTING 27.2 Manipulating Arrays

<?php
 $myarr = array(1, 2, 3, 4);
 $myarr[4] = "Hello";
 $myarr[] = "World!";
 $myarr["elephant"] = "Wombat";
 $myarr["foo"] = array(5, 6, 7, 8);
 echo $myarr[2];
 echo $myarr["elephant"];
 echo $myarr["foo"][1];
 $myarr = array();
?>

The initial array is created with four elements, assigned the values 1, 2, 3, and 4. Because no keys are specified, PHP automatically assigns keys for us starting at 0 and counting upward—giving keys 0, 1, 2, and 3. Then we add a new element with the [] operator, specifying 4 as the key and "Hello" as the value. Next, [] is used again to add an element with the value "World!" and no key, and then again to add an element with the key "elephant" and the value "wombat". The line after that demonstrates using a string key with an array value — an array inside an array (a multidimensional array).

The next three lines demonstrate reading back from an array, first using a numeric key, then using a string key, and then using a string key and a numeric key. Remember, the "foo" element is an array in itself, so that third reading line retrieves the array and then prints the second element (arrays start at 0, remember). The last line blanks the array by simply using array() with no parameters, which creates an array with elements and assigns it to $myarr.

The following is an alternative way of using array() that enables you to specify keys along with their values:

$myarr = array("key1" => "value1", "key2" => "value2", 7 => "foo", 15 => "bar");

Which method you choose really depends on whether you want specific keys or want PHP to pick them for you.

Оглавление книги


Генерация: 0.062. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз