Page 147 - Open Soource Technologies 304.indd
P. 147
Unit 6: Arrays
Thearray[2]: <? print $Thearray[2];?> Notes
Thearray[3]: <? print $Thearray[3]; ?>
Thearray[4]: <? print $Thearray[4]; ?>
Thearray[5]: <? print $Thearray[5]; ?>
Thearray[6]: <? print $Thearray[6]; ?>
Thearray[7]: <? print $Thearray[7]; ?>
Thearray[8]: <? print $Thearray[8]; ?>
Thearray[9]: <? print $Thearray[9]; ?>
</pre>
Resulting page
Thearray(0): Zero
Thearray(1): pne
Thearray(2): two
Thearray(3): three
Thearray(4): four
Thearray(5): five
Thearray(6): six
Thearray(7): seven
Thearray(8): eight
Thearray(9): nine
In this example the array has been defined as comma separated element (the first element in the
array is element 0 !). The array command has been used to define the array.
We may want to generate an array from a data in a text. In the example below, each word in a
text will be stored in an array (one word in each element of the array), and then the array will
be printed out (with commandprint_r), and finally word number 5 will be shown:
Example:
<pre>
<?
$TheText=”zero one two three four five six seven eight nine”;
$Thearray=split (“ “,$TheText) ;
print_r ($Thearray);
?>
<hr>
The element number 5 is : <? print $Thearray[5]; ?>
</pre>
Resulting page
Array
(
LOVELY PROFESSIONAL UNIVERSITY 141