Arrays in PHP

In PHP array function is used to create an array. Array is a mapping of keys to values. A key can hold value or another array, key can be either an integer or a key.
Example 1 :

<?php
		$array = array ('a' => 'one', 'b' => 'two', 'c' => 'three' );
		foreach (  $array  as $key => $value ) {
			echo “key =  $key<br>”;
	                        echo “value = $value<br>”;
		}
            ?>

foreach is a function used for iterate over arrays.
Example 2:

<?php
		$array = array (1 =>  'First value', 2 => 'second value' ,
		'this holds array' => array ( 0 => 'a', 1 => 'b' ) ,
                         'this holds a value' => 'last value' );
		echo $array[1] ; // out put will be First value.
		echo $array['this holds array'][0] ;    // out put will be 'a'
                        print_r($array);
                        echo 'Number of elements of the array = ' . count ( $array );
         ?>

print_r prints array, it is a very useful function for debugging. count function is used to get the number elements of a array. Both functions require array name as a parameter.

Next Page »