Sunday, February 2, 2014

Types of Array in PHP Programming

8:35 AMSunday, February 2, 2014

Types of array: - In real world we have two types of array. These are following :-

Single dimensional array: -
                                             In this array data items stored in only in one dimension.
Like $word [1] =”hello” which means the array $word stores the items in one dimension such as $word [1] $word [2] $word [3] $word [5] etc. The arrays which we have discussed in previous topics all are single dimensional array.

Multidimensional Arrays: -
                                             If an array is able to store the items at more than one dimensional is called multidimensional array. Multidimensional array comes in picture where we need to store different association with single variable. For example if we want to make a matrix of size 2x2 the array representation will be.

$mat[ 0] [0]=2
$mat[ 0] [1]=4
$mat[ 1] [0]=7
$mat[ 1] [1]=6

Here the array $mat use two direction first is for row and second for column. So this is called two dimensional array. We can create any dimensional array in PHP . Such that

$threeDarray[ ][ ][ ]
$fourDarray[ ][ ][ ][ ]
$fifthDarray[ ][ ][ ][ ][ ]
$nDarray [ ][ ][ ][ ][ ][ ][ ]……..[ ][ ]

Concept of multidimensional array can be understood with the help of example:-
$shop= array(‘fruit’ =>
array(‘red’ => ‘apple’,
‘orange’ => ‘orange’,
‘yellow’ => ‘banana’,
‘green’ => ‘pear’),
‘flower’ =>
array(‘red’ => ‘rose’,
‘yellow’ => ‘sunflower’,
‘purple’ => ‘iris’));

It is simply an array with two values stored in association with keys. Each of these values is
an array itself. After we have made the array, we can reference it like this:-
if we want to check which  fruit of color red then we use

$shop [fruit][red]; this statement return the value “apple”.
Protected by Copyscape Online Copyright Protection Software


Post a Comment

Toggle Footer