02 February 2017

Short Note on Types of Array in PHP

php

There are two types of Array in PHP. They are:
1) Numeric Array
2) Associative Array

1) Numeric Array:

This kind of array use index number to retrieve data. An element can be added to this array without setting any index number. The index will be auto-incremented to that element.

The indexing always starts from zero ( 0 ).

Example:

<?php
//USE OF NUMERIC ARRAY IN PHP
$myArray = array('zahid','bin');
echo "This is Value at index0:  " . $myArray[0];
echo "<br/>";
echo "This is Value at index1:  " . $myArray[1];
?>

2) Associative Array :

This kind of array use string values to retrieve data. A string index should be provided while creating a new element in array. The main use of associative array is memoization.

[Note: memoization is a technique to optimize performance and avoiding repeat of calculations which has been done once in previous results.]

Associative Array is useful for mapping data and searching.

Example:


<?php
//USE OF ASSOCIATIVE ARRAY IN PHP
$addressbook = array('zahid'=>'+88015500000000','manager1'=>'00002211');
echo "This is Zahids number " . $addressbook['zahid'];
?>



Whats new:



(If you found this article useful then share with your friends.)

No comments: