http://www.chrispearson.org/pages/programming/php/array02.asp
11h35
Wednesday, 7. January 2009

PHP: ARRAYS

CREATING ARRAYS

The formally defined way to create an array in PHP code is to use array(). Although array() looks like a function it is a language construct, like echo().

It's probably best to start with the most formal syntax.

There are other ways to code the task but these are less explicit.

SIMPLE, INDEXED LIST

To begin with we will use array() to create an array of a version the shopping list we looked at earlier

  bread
  lemonade
  apples
  potatoes
  coffee

 

we can use array() to assign values to a simple indexed array using

$shoppinglist = array("bread", "lemonade",
   "apples", "potatoes", "coffee");

where bread will have the index 0 (zero, that is), lemonade 1, apples 2, grapes 3 and coffee 4.

Notice the we would get exactly the same result using slightly different code:

$shoppinglist = array(0 => "bread",
   1 => "lemonade",
   2 => "apples",
   3 => "potatoes",
   4 => "coffee");

Here it is more obvious how the indices are applied to the elements we're storing in the array. Notice, also, that placing different parts of the text on different lines doesn't affect the function but it does make the text easier to read.

Having the option to specify the key like this gives us an insite into how more complex keys can be used in creating an associative array.

Numbering indices

Notice that indices of PHP arrays start at zero, not one. This is a common concept in processing lists and strings in most programming languages.

In PHP the same principle is applied to string processing, where the first character in a string is character zero - In the string Rock and Roll, the character R appears in positions 0 and 9, l appears in positions 11 and 12 and the string is 13 characters long.

We could, for instance, categorise our shopping list in the array $shoppinglist like this

$shoppinglist = array("bakery" => "bread",
   "drink" => "lemonade",
   "fruit" => "apples",
   "vegetables" => "potatoes",
   "beverage" => "coffee");

In this instance the keys and their associated values would be

Key Value
bakery bread
drink lemonade
fruit apples
vegetables potatoes
beverage coffee

As a final step before we look at getting elements out of arrays and actually using them for something, we can code the shopping list into a PHP web page:

 
Open a new document in your HTML editor, calling it shopping.php A new page in the HTML editor, saved as shopping.php
Start by creating the HTML page framework, starting with the HTML tag pair and creating a head and body for the document

<head>
< title>A shopping list</title>
< /head>
<body>
</body>
< /html>

Add a line of text and then the code to initialise the shopping list array

<head>
< title>A shopping list</title>
< /head>
<body>
The shopping list<BR>
< ?PHP
$shoppinglist = array("bread", "lemonade",
"apples", "potatoes", "coffee");
?>
</body>
< /html>

Save the file  
Just to see what happens, add a print to the PHP code <head>
< title>A shopping list</title>
< /head>
<body>
The shopping list<BR>
< ?PHP
$shoppinglist = array("bread", "lemonade",
"apples", "potatoes", "coffee");
print("The list array is $shoppinglist<BR>\n");
?>
</body>
< /html>
 

Save the file and look at it in a browser (You'll need to access the page through a web server to process the PHP: Use the format http://localhost/shopping.php and not a physical path like C:\intepub\shopping.php)

Here's one I prepared earlier: shopping.php

Which may whet your appetite for the next page: Accessing arrays

Back to the top of this page

 

< Previous page: Introduction Next page: Accessing arrays>

xxx,xxx

copyright ©2000 - 2009 Chris Pearson