
|
|
|||
Introduction to PHP ProgrammingBy PHP Catalyst2007-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 1Ok, 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 2Look 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 3Ok, 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 4In 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 5In 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 6Determining 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 7Tutorial 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 |
||||
| About the NetVisits, Inc Network | Write For Us | Advertise Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy. |
Visit other NetVisits, Inc. sites: |