Wednesday 26 March 2014

Arrays in PHP | Chapter 3

 - Indexed arrays - Arrays with numeric index or numeric keys.

Understanding Numeric Index Number -  Every value in the array is assigned with an index number. Below image shows how an index number is assigned to a set of values. The first value is assigned with a index value of 0 as the index number always starts with Zero.


php indexed arrays












Example -

<?php

$numbers = array("one","two","three");

for($i=0; $i<sizeof($numbers); $i++) {

echo $numbers[$i]."<br>";

}

?>

Output:

one
two
three




  - Associative arrays - Arrays with string index values or keys. Here we can manually define the value as keys instead of 0's & 1's which we saw in Indexed arrays.The figure below includes the age of few candidates in an array, going by the indexed array will not be the best choice, instead we can use their names as the key. Accordingly we can associate the key to their ages.


php associative arrays