types-of-array

Types of array in PHP: Indexed, associative and multidimensional array

Share on FacebookTweet about this on TwitterShare on Google+Pin on PinterestShare on RedditDigg thisShare on TumblrPrint this page

types-of-array

1: Indexed arrays

This is simple type of array which holds a set of elements, whenever an element is added to indexed array a numeric key gets associated to it, that can be used to display that particular element.

How to create an Indexed array

This will create a simple indexed array. Most people get confused that why the numbers are there when they never assigned them. The answer is yes you never assigned them but by default every array element always has a key assigned to it doesn’t matter what types of array is it.

 How to display all elements of an Indexed array

Result:

 

2: Associative Array

Associative arrays are the one where a key is manually assigned to an array element. The difference between Indexed array and associative array is that in Indexed array we don’t manually assign a key to the array although it will have default numeric keys but in Associative array we manually assign keys to the array.

Indexed Array

Array
(
[0] => apple
[1] => banana
[2] => Guava
[3] => orange
)

Associative Array

Array
(
[name] => apple
[color] => red
[type] => fruit
[healthy] => true
)

How to create an Associative Array

 

 

How to display all elements of an Associative array

Results:

 

3: Multidimensional Array

A multidimensional array can have array inside an array to many levels deep, it can be a 2 dimensional array (i.e array inside an array), 3 dimensional array (i.e array inside an array that is also inside an array) , 4 dimensional array (i.e  yeah you guessed it right array inside an array that is inside an array and that is inside one) and so on 5d , 6 d …..

How to create an Multidimensional Array

How to display all elements of a Multidimensional array.

Results:

The following two tabs change content below.
Loves: " The Technology", Learning new stuff not necessarily tech related, acquires self proclaimed "Good sense of humor". Apart from Coding I do like Reading,Sketching observing other people, exploring whatever i come across and Going Places. More About Me