Chapter05 Array
Chapter05 Array
array
*Acknowledgement : Special thanks to Azam Amin (sekrets.net) for his numerous contribution in this chapter.
In computer science an array is a data structure consisting of a group of elements that are
accessed by indexing. In most programming languages each element has the same data
type and the array occupies a contiguous area of storage. Most programming languages
have a built-in array data type.
Well as we can see here guys, array can be divided into 3 categories:
• Numeric array
• Associative array
• Multidimensional array
We’re going to define one by one of the categories that provided above with the example for
each of it.
Numeric array
A numeric array stores each element with a numeric ID key. Using the numeric ID, the index
of the array can be stored using 2 ways. The below example is automatically assigned into
the array.
Example 1:
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 5:1
Chapter 5: PHP Array
You can see the variable $names that used as array that access by its element. This is how
when a string have been initialize into the array. Because array by default is empty and to be
cleared every array is start from 0.If we declared the array with the size of 9 actually the max
for it is only 8.Because all array is start from 0.Next using the same technique we see the
second example.
Example 2:
As you can see, elements in array can be any type scalar of data (string, number, variable)
and so on. So, here the advantage when using array. You can initialize any type data into it
as long it follow the rule using the array.
The second way stored the data in numeric ID can be done manually .As example below:
Example 1:
As we look the example above , every elements were initialize using the manual method.
“peter” was initialize to Array number 1 aka names[0] and so on . So that’s the example how
it done using manual method.
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 5:2
Chapter 5: PHP Array
Output :
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 5:3