# Initializing Arrays PHP

In the situations where you want to fill an array with several values before you use it, it can become cumbersome to write an assignment for each element. PHP offers the array function to help in this matter. It takes a list of values and returns an array. uses array to build an rray of the months of the year.

Each value is just as it would be if it were on the right side of the assignment operator. Commas separate the values. By default, as with using empty brackets, elements will be numbered starting at zero. You can override this by using the => operator. In I have set January to have the index 1. Each subsequent element is indexed by the next integer.

You aren't limited to setting the index for the first element, of course. You can assign the index for every element. And you aren't limited to assigning integers as indexes. builds an array for translating various ways to write a month into a single form.

Initializing an Array

<?
$monthName = array(1=>"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); print("Month 5 is$monthName[5] <BR> ");
?>

Using an Array to Translate Values

<?
$monthName = array( 1=>"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", "Jan"=>"January", "Feb"=>"February", "Mar"=>"March", "Apr"=>"April","May"=>"May", "Jun"=>"June", "Jul"=>"July", "Aug"=>"August","Sep"=>"September", "Oct"=>"October", "Nov"=>"November","Dec"=>"December","January"=>"January", "February"=>"February", "March"=>"March", "April"=>"April", "May"=>"May", "June"=>"June","July"=>"July", "August"=>"August", "September"=>"September", "October"=>"October", "November"=>"November", "December"=>"December"); print("Month 5 is " .$monthName[5] . "<BR> ")
print("Month Aug is " . $monthName["Aug"] . "<BR> "); print("Month June is " .$monthName["June"] . "<BR> ");
?>