spacer
Web Development Tutorials PHP Tutorials
 Developer Newsletter

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn AJAX
Learn JavaScript
Learn Pear
Free White Papers

Developer Resources
Developer Tools
Developer Content
Survey Software
Dedicated Servers




Introduction to PHP Programming

By PHP Catalyst
2007-11-19


Creating Arrays in PHP

Creating an array in PHP is a simple task. An array() construct is used in PHP to create an array. Here are couple of examples with explanation for better understanding:

We first start with creating an Indexed or Numerical Array which is quite simple. Remember, all you have to do is use the array() construct. In this example, we will create an indexed array consisting of some protocols (our data elements). The position of first indexed element will be by default zero unless we change it.

<?php //Example 1
$protocols = array("TCP""IP""DNS""FTP""SMTP");
//TCP, IP, DNS, FTP and SMTP are data elements of the array
?>

Ok, we have got the array created but how do we know what position are the elements stored inside the array? How do we know what elements are actually stored in the array? It's simple. We will have to make use of print_r() construct in PHP which make our job easy. Here's how:

<?php //Example 2
$protocols = array("TCP""IP""DNS""FTP""SMTP");
print_r($protocols); //using the print_r() construct
//Output: Array ( [0] => TCP [1] => IP [2] => DNS [3] => FTP [4] => SMTP ) 
?>

Look at the output of above code. At the first position is TCP and the first position is having an integer value of zero which is our key to this array. We used print_r() to print the elements of the array and their positions. If the data elememnts stored are very large in number consider not using the print_r() as it will fill up your screen with its output. Now let's take a look at another way of creating the same array in example #2.

<?php //Example 3
$protocols[0] = "TCP";
$protocols[1] = "IP";
$protocols[2] = "DNS";
$protocols[3] = "FTP";
$protocols[4] = "SMTP";
print_r($protocols);
//Output: Array ( [0] => TCP [1] => IP [2] => DNS [3] => FTP [4] => SMTP )
?>

Ok, we are now familiar with both the ways of creating an indexed array, now let's take a look at creating an Associative array. Remember, in associative array, the key value used for storing the position of an elements inside the array is a string but not an integer.

<? //Example 4
$protocol['TCP'] = "Transmission Control Protocol";
$protocol['DNS'] = "Dynamic Naming System";
$protocol['IP']  = "Internet Protocol";
$protocol['FTP'] = "File Transfer Protocol";
print_r($protocol);
//Output: Array ( [TCP] => Transmission Control Protocol 
//[DNS] => Dynamic Naming System [IP] => Internet Protocol 
//[FTP] => File Transfer Protocol ) 

//Associate array creation can also be done using array() construct

$months = array('Jan'=>"January"'Feb'=>"February"'Mar'=>"March");
print_r($months);
//Output: Array ( [Jan] => January [Feb] => February [Mar] => March )

echo $months['Jan']; //Referring an individual element of the array
//Output: January
?>

In an associative array we refer to position of a stored data element by the key of the index which is a string. But then, you can always have a string like "01", "02" as the key or index for elements of an array. It's just that "01" and "02" are considered as strings but if all the key values were mentioned as integers such as 1, 2 ,3 ,4 then the index will consist of all numerical values which will make it an indexed array.

There are certain requirements some times due to which you may not want an indexed array to have it's starting index number as zero instead you may want to begin your index at 1. Such is a requirement when you want to store all months in an array and we all know month numbers starts with 1 and not zero. Here is how we do it:

<? //Example 5
$months = array(1=>"Jan""Feb""Mar""Apr""May""June""July""Aug",
"Sep""Oct""Nov""Dec");
print_r($months);
//Output: Array ( [1] => Jan [2] => Feb [3] => Mar [4] => Apr [5] => May 
// [6] => June [7] => July [8] => Aug [9] => Sep [10] => Oct 
// [11] => Nov [12] => Dec ) 
?>

In the above example we manually specified numerical value of the first position in the array. The remaining values were automatically incremented based on the first key value. You can mention any other number as the first key value and not necessarily it has to start with 0 or 1. Only in case of an indexed array where a starting key value is not specified, it's automatically started with a zero.

Now that we are familiar with creation of both indexed and associate arrays, let's look at some common and simple tasks related to arrays.

How to assign a range of values to an array?

To accomplish this task we will make use of the range() function, usage of which is fairly simple and straight. Check this example:

<? //Example 6
$alphabets range('a','z'); 
// The above will hold all alphabets from a to z, key starts at 0

$numbers range (1100);
//All numbers from 1 to 100, key starts at 0
?>

Determining the size of an array?

Often, you would come across some or the other requirement in your coding to first find the size of an array (number of elements) and then do some looping to retrive values or perform some other array related operation. Use count() or sizeof() functions for this purpose. It's simple:

<? //Example 7
$alphabets range('a','z'); 
// The above will hold all alphabets from a to z, key starts at 0
echo count($alphabets); //Output: 26

$numbers range (1100);
//All numbers from 1 to 100, key starts at 0
echo count($numbers); //Output: 100
?>
Let's take a look at some very basic and most commonly used array related tasks in next topic Array Operations.

Tutorial Pages:
» What can I do with PHP?
» Popoular Features of PHP
» Basics of PHP
» Variables in PHP
» Data Types in PHP
» Expressions and Operators
» Control Structures in PHP
» Functions in PHP
» Declaring Functions in PHP
» Scope of a Variable
» Built-in Functions in PHP
» Handling Strings in PHP
» Printing Strings in PHP
» String Comparisons in PHP
» Manipulating Strings in PHP
» Arrays in PHP
» Types of Arrays in PHP
» Creating Arrays in PHP
» Array Operations in PHP


 | Bookmark Print |   Write For Us
Related Tutorials:
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK
» Installing PHP on Windows



About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: