- Listing of String Functions
- Using the String Functions
- Formatting Strings
- Converting to and from Strings
- Creating Arrays
- Modifying Arrays
- Removing Array Elements
- Looping Over Arrays
- Listing of the Array Functions
- Sorting Arrays
- Navigating through Arrays
- Imploding and Exploding Arrays
- Extracting Variables from Arrays
- Merging and Splitting Arrays
- Comparing Arrays
- Manipulating the Data in Arrays
- Creating Multidimensional Arrays
- Looping Over Multidimensional Arrays
- Using the Array Operators
- Summary
Creating Arrays
It's time to take the next step up in PHP sophistication: arrays. Arrays are collections of values stored under a single name, and they're a big part of PHP work. You use arrays when you have a set of data to work with, such as the test scores of a set of students. Arrays are easy to handle in PHP because each data item, or element, can be accessed with an index value.
You can create arrays by assigning data to them, just as you do with other variables. You can give arrays the same names as you give to standard variables, and like variable names, array names begin with a $. PHP knows you're working with an array if you include [] after the name, like this:
$fruits[1] = "pineapple";
This creates an array named $fruits and sets the element at index 1 to "pineapple". From now on, you can refer to this element as you would any simple variable—you just include the index value to make sure that you reference the data you want, like this:
echo $fruits[1];
This statement echoes "pineapple". And you can add new values with different numeric indexes:
$fruits[2] = "pomegranate"; $fruits[3] = "tangerine";
Now you can refer to $fruits[1] (which is "pineapple"), $fruits[2] (which is "pomegranate"), and $fruits[3] (which is "tangerine"). In this case, we've stored strings using numeric indexes, but you can also use string indexes. Here's an example:
$apple_inventory["Pittsburgh"] = 2343; $apple_inventory["Albany"] = 5778; $apple_inventory["Houston"] = 18843;
You can refer to the values in this array by string, as $apple_inventory["Pittsburgh"] (which holds 2343), $apple_inventory["Albany"] (which holds 5778), and $apple_inventory["Houston"] (which holds 18843). Note that in PHP, the same array can have both numeric and text indexes, if you want to set things up that way.
There's also a shortcut for creating arrays—you can simply use [] after the array's name. Here's an example:
$fruits[] = "pineapple"; $fruits[] = "pomegranate"; $fruits[] = "tangerine";
In this case, $fruits[0] will end up holding "pineapple", $fruits[1] will hold "pomegranate", and $fruits[2] will hold "tangerine".
PHP starts numbering array elements with 0. If you wanted to loop over all the elements in this array, you'd start with 0, as in this for loop—note that you use the count function to find the number of elements in an array:
for ($index = 0; $index < count($fruits); $index++){ echo $fruits[$index], "\n";}
Here's an even shorter shortcut for creating an array, using the PHP array function:
$fruits = array("pineapple", "pomegranate", "tangerine");
This also creates the same array, starting from an index value of 0. What if you wanted to start with an index value of 1? You could specify that with => like this:
$fruits = array(1 => "pineapple", "pomegranate", "tangerine");
Now the array would look like this:
$fruits[1] = "pineapple"; $fruits[2] = "pomegranate"; $fruits[3] = "tangerine";
You can create arrays with text as index values in the same way:
$apple_inventory = array("Pittsburgh" => 2343, "Albany" => 5778, "Houston"] => 18843);
This creates the following array:
$apple_inventory["Pittsburgh"] = 2343; $apple_inventory["Albany"] = 5778; $apple_inventory["Houston"] = 18843;
The => operator lets you specify key/value pairs. For example, "Pittsburgh" is the key for the first element, and 2343 is the value.