0% found this document useful (0 votes)
47 views

Chapter05 Array

This document discusses PHP arrays. It defines an array as a data structure that stores elements accessed by index. Arrays can be numeric, associative, or multidimensional. Numeric arrays store elements with numeric IDs that can be assigned automatically or manually. Elements in an array can be of any scalar data type like strings, numbers, or variables. The document provides examples of declaring numeric arrays using both automatic and manual ID assignment and accessing array elements by ID.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Chapter05 Array

This document discusses PHP arrays. It defines an array as a data structure that stores elements accessed by index. Arrays can be numeric, associative, or multidimensional. Numeric arrays store elements with numeric IDs that can be assigned automatically or manually. Elements in an array can be of any scalar data type like strings, numbers, or variables. The document provides examples of declaring numeric arrays using both automatic and manual ID assignment and accessing array elements by ID.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Chapter 5: PHP 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.

Some programming languages support array programming which generalizes operations


and functions to work transparently over arrays as they do with scalars, instead of requiring
looping over array members.

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

When we discuss about numeric ID it can be done using 2 ways. It is :

• Assign using Automatic method


• Assign using Manual method

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

The ID key can also be used in script .As example below :

Output :

PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 5:3

You might also like