PHP Tutorials – Arrays Part 1

October 20, 2008 by admin  
Filed under PHP, Recent Tutorials

High Quality PHP Tutorial Videos – Taught by Experts
We also have extensive PHP Tutorials in high quality video format. These are ideal for beginners who need to master PHP quickly
Title / Free Demo :
Beginners PHP Tutorial Videos
Author:
Mike Morton
Duration:
6 Hours – Lessons: 86

PHP Arrays – Basics

This tutorial covers creating Arrays – Suitable for all versions of PHP

Introduction to the PHP Array

The array data structure provides a convenient way to store multiple values in a single variable. An Array uses pointers or index keys to map each value stored within it. The type of pointer, and the syntax for accessing it, varies depending upon the array type used. An array is permitted to store other arrays as values. Here is a look at both of PHP’s array types and how they are created and used.

Indexed Array
An indexed array uses integers as the key. The Array index begins at 0. PHP imposes no limit on the maximum number of elements an array may contain, so there is no maximum ending integer. Of course, array size is subject to the memory limitations established in the PHP.ini file as well as of the server where PHP is running.

Associative Array

Associative arrays use string values as keys.

Creating an Indexed Array

An array can be created simply by storing a value to a variable that uses the array structure.

Example:


Select All

$mystaff[0]=’Matthew’;

The above method creates an indexed array. This method is often used when the array will be built with values that can not be pre-determined or that will be created on the fly.

If the values of the array are known ahead of time, you could use this syntax:

Example:


Select All

$mystaff = array(0=>’Matthew’, 1=>’Mark’, 2=>’Luke’);

In the above example, the array has 3 elements, even though the last element’s key is the integer 2. New PHP programmers may forget the indexed array key structure is zero-based, hence the 3rd element in the above example is referanced by 2 and not 3.

Both indexed array creation methods are interchangeable. You do not have to use one method under any particular circumstance.

Displaying an Indexed Array

The value of an indexed array element is displayed by referencing the array element by its key.

Example:

Select All

echo "The names of my three staff members are:
$mystaff[0] . ", " . $mystaff[1] . ", " . $mystaff[2];

Give it a try, create a new document enter the following code


Select All

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//
EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<title>Arrays</title>
</head>
<body>
<?php   $mystaff = array(0=>’Matthew’,   1=>’Mark’,   2=>’Luke’);
 <p>echo "The names of my three staff members are: ".   $mystaff[0] . ",
" . $mystaff[1] . ", " . $mystaff[2]; ?>
</body> </html>

>and save it as array-test.php, upload it to your web server and view it.

Creating an Associative Array

An associative array uses a string value as the key. Where an indexed array is typically used to store strings, an associative array is typically used to store numeric values. However, it is entirely up to you which array type you use. These are guidelines, not rules.

In this example below, we will create an associative array containing salary information for those three staff members we created in the indexed array example above.

Example

$salaries["Matthew"] = 65000;
$salaries["Mark"] = 75000;
$salaries["Luke"] = 100000;

Instead of declaring each array value on its own line, you could also use this syntax:


Select All

$salaries=array("Matthew"=>65000, "Mark"=>75000, "Luke"=>100000);

Displaying an Associative Array

Select All

echo "Matthew’s salary is: $" . $salaries["Matthew"] . "". "Mark’s salary is:
$" . $salaries["Mark"] . "".
"Luke’s salary is: $" . $salaries["Luke"];

Give it a try, delete the contents of your array-test.php page and replace it with the follow:

Select All

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//
EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<title>Arrays</title>
</head>
<body>
<?php   $salaries=array("Matthew"=>65000,   "Mark"=>75000,
"Luke"=>100000); echo "Matthew’s salary is: $" . $salaries["Matthew"] . "".
 "Mark’s salary is: $" . $salaries["Mark"] . "".
  "Luke’s salary is: $" . $salaries["Luke"]; ?>
</body>   </html>

After saving your page, upload to your webserver and view. You know have a fundamental knowledge of creating and displaying Arrays in PHP.

Summary

Arrays are powerful tools, especially when used with the for and while functions. There are other PHP functions designed especially for working with arrays. They provide convenient methods for sorting, transversing, and manipulating arrays. We’ll look at these in next PHP Array tutorial.

Comments

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!

You must be logged in to post a comment.