HOME
PHP
Introduction Php Installation
PHP Basic Scripts
PHP variables
PHP operators
PHP Conditional statement
PHP functions
PHP Arrays
PHP Objects & classes
PHP with Forms
PHP Files
PHP With Database
PHP session
PHP cookies
PHP Regular Expressions
PHP Server Environment
PHP Graphics
PHP Advance
|
PHP Arrays
In computer programming, an array, also known as a vector or list, is one of the simplest data structures. Arrays hold equally-sized data elements, generally of the same data type. Individual elements are accessed by index using a consecutive range of integers, as opposed to an associative array. Some arrays are multi-dimensional, meaning they are indexed by a fixed number of integers, for example by of four integers. Generally, one- and two-dimensional arrays are the most common.
An array is series of variables (or objects) that are of the same type and size. Each of these variables (objects) are indexed; individual elements are called array elements. Arrays can be used in SQL and PL/SQL programs to reduce programming time and improve performance.
Each item in an array is commonly called as element.
There are three different kind of arrays:
Numeric array - An array with a numeric ID key
Associative array - An array where each ID key is associated with a value.
Multidimensional array - An array containing one or more arrays
Numeric Arrays
A numeric array stores each element with a numeric ID key.numerically added arrays are usefull doe storing values in the order
in which they were added accoring to a short pattern
Example 1
In this example the ID key is automatically assigned:
$names = array("name1","address","phone");
Example 2
In this example we assign the ID key manually:
$names[0] = "name1";
$names[1] = "address";
$names[2] = "phone";
The ID keys can be used in a script:
| |
| output
name1 address phone numbers are information about the person
|
Associated Array
An associated array is indexed with strings between the square bracets rather than numbers.
Associative Arrays An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical array is not always the best way to do it.
With associative arrays we can use the values as keys and assign values to them.
Example 1
In this example we use an array to assign Phone numbers of the different persons:
$phones = array("john"=>801-222-222, "hari"=>966-222-222, "rama"=>666-222-111);
Example 2
This example is the same as example 1, but shows a different way of creating the array:
$phones['john'] = "801-222-222";
$phones['hari'] = "966-222-222";
$phones['rama'] = "666-222-111";
The ID keys can be used in a script:
| |
|
|
The code above will output:
john's phone number is 801-222-222 !!!!!!.
|
Multidimensional Arrays
In a multidimensional array
each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.Arrays that contains arrays as their elements are known as multdimensional array
Example
In this example we create a multidimensional array, with automatically assigned ID keys:
| |
|
The array above would look like this if written to the output:
|
|
|
|