ARRAYS with PHP R. B. see chapter 7, Ullman.

2013 年 2 月 16 日3740

1

ARRAYS with PHP R. B. see chapter 7, Ullman.

An array is a variable that contains multiple values which can be strings, numbers or other arrays.

To create an array:

1) $fruit = array (鈥渁pples鈥, 鈥減ears鈥, 鈥渙ranges鈥, 鈥減lums鈥)

2) $fruit = array(); // initialize array use round brackets

$fruit[0] = 鈥渁pples鈥; // assign key to each array element

$fruit[1] = 鈥減ears鈥;

$fruit[2]= 鈥渙ranges鈥;

$fruit[3]= 鈥減lums鈥;

3) Associative Array 鈥 you associate a string or other value 鈥 you don鈥檛 use serial

numbers

$provincialcapital[鈥淓dmonton鈥漖;

$provincialcapital[鈥淭oronto鈥漖;

$provincialcapital[鈥淗alifax鈥漖;

or

$soups = array(

鈥淢onday鈥 => 鈥淐lam Chowder鈥,

鈥淭uesday鈥 =>鈥漌hite Chicken鈥,

鈥淲ednesday => 鈥淰egetarian鈥

);

If you try to output the array elements by using the echo or print statement:

<?php

print 鈥$soups <br />\n鈥;

?>

You will only see the word 鈥淎rray鈥 on the screen the individual values are not echoed to the

screen.

2

ADDING MORE ELEMENTS TO AN ARRAY - COUNT() function

If you want to see how many elements are in an array you can use the count() function like so:

<?php

$soups = array(

"Monday" => "Clam Chowder",

"Tuesday" => "White Chicken",

"Wednesday" => "Vegetarian",

);

print "$soups <br>\n";

$howmany = count($soups);

print "$howmany";

?>

it will display 3 in this case.

To add more elements to the array:

$soups[鈥淭hursday鈥漖 = 鈥淐hicken noodle鈥;

$soups[鈥淔riday鈥漖 = 鈥淭omato鈥;

$soups[鈥淪aturday鈥漖 = 鈥淐ream of Broccoli鈥;

$nowhomany = count($soups);

print "$nowhowmany"; // this will output 6 to the screen now.

MERGING ARRAYS array_merge() function

The array_merge() function is new to PHP4 鈥 it allows you combine elements of different arrays

into one array.

<?php

$fruits = array("apples", "oranges", "pears");

$vegetables = array("potatoes", "celery", "carrots");

$newarray = array_merge($fruits, $vegetables);

$howmany = count($newarray);

print "$howmany"; // will output 6 to the screen

?>

Note the order you merge the arrays in will determine the order they appear i.e.

array_merge($fruits, $vegetables) will be different from array_merge($vegetables, $fruits).

3

ACCESSING ARRAY ELEMENTS

To access individual array elements you refer to them by their index number which usually starts

at 0 but you can assign the array to start at any number. With an associative array you refer to

the entire element e.g. $soups[鈥淢onday] - not the use of the square brackets.

e.g

in the previous example to view the first vegetable in the array add the code:

print 鈥$vegetables[0]鈥; // this will print to the screen potatoes

print 鈥$vegetables[1]鈥 ; // this will print to the screen celery

You could also assign the array element to a separate variable e.g.

$veg = $vegetables[1];

print 鈥$veg鈥; // will output celery to the screen

One of the limitations of arrays is that in order to refer to a specific element you must know the

keys to which the specific element refers to and in the case of associate arrays $soups[1] will not

point to nothing since its value is a string, i.e. $soups[鈥淢onday鈥漖

FOR LOOPS

To output or access all the elements of an array you can use loops e.g.

<?php

$vegetables = array("potatoes", "celery", "carrots");

for ($i=0; $i< count($vegetables); $i++)

{

print "$vegetables[$i]<br>\n";

}

?>

In this example we used $i < count($vegetables); we could have used $i < 4 - but that requires

you know exactly how many elements are in the array.

The screen output will be:

potatoes

celery

carrots

4

Using the each() function to reveal the key and value elements in an array

<?php

$vegetables = array("potatoes", "celery", "carrots");

for ($i=0; $

0 0