PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
Creating Arrays - Examples
This section provides a tutorial example on how to create arrays in different ways.
Here is a tutorial example PHP script to show you some of array rules described in the previous section:
<?php # ArrayTest.php #- Copyright 2003 (c) HerongYang.com. All Rights Reserved. # print "\nLibrary hours:\n"; $libraryHours = array( 'Monday - Friday'=>'09:00 - 21:00', 'Saturday'=>'09:00 - 17:00', 'Sunday'=>'closed'); print_r($libraryHours); # print "\nPrime numbers:\n"; $primeNumbers = array(3, 5, 7, 11); $primeNumbers[] = 13; print_r($primeNumbers); # print "\nFruits:\n"; $fruits[] = 'Apple'; $fruits[] = 'Orange'; $fruits[''] = 'Peach'; $fruits[''] = 'Banana'; $fruits['G'] = 'Grape'; $fruits['7'] = 'Pear'; $fruits[] = 'Fig'; print_r($fruits); # print "\nFruits array modified:\n"; unset($fruits[1]); $fruits[1] = 'Orange'; print_r($fruits); ?>
If you run this sample script, you should get:
herong> \php\php ArrayTest.php Library hours: Array ( [Monday - Friday] => 09:00 - 21:00 [Saturday] => 09:00 - 17:00 [Sunday] => closed ) Prime numbers: Array ( [0] => 3 [1] => 5 [2] => 7 [3] => 11 [4] => 13 ) Fruits: Array ( [0] => Apple [1] => Orange [] => Banana [G] => Grape [7] => Pear [8] => Fig ) Fruits array modified: Array ( [0] => Apple [] => Banana [G] => Grape [7] => Pear [8] => Fig [1] => Orange )
The behavior of the $fruits array is very interesting. Review it carefully.
Table of Contents
Introduction and Installation of PHP
PHP Data Types and Data Literals
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Array Related Built-in Functions
Usage Examples of Array Functions
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS