Why should you care array?
If you need to add a month name (ex. January) in your program, you’ll declare a variable name and assign the name in it. Like this-
<?php $month=”January”; ?>
Note, January is surrounded by quotes, it is a string type data.
Now, if you need to add all the month names, what you’ll do? You can do it in 2 ways-
Method 1: Declare 12 different variable names and assign month names in those variables. Like this-
<?php $month=”January”; $month2=”February”; $month3=”March”; $month4=”April”; $month5=”May”; $month6=”June”; $month7=”July”; $month8=”August”; $month9=”September”; $month10=”October”; $month11=”November”; $month12=”December”; ?>
But, it is a boring and time consuming task. There is a better solution, using method 2.
Method 2: Change the data type from string to array, add month names in it and assign it in a variable in the following way –
<?php $month=array(“January”, “February”, “March” , “April” , “May” , “June” , “July” , “August” , “September” , “October” , “November” , “December”); ?>
What is an array?
An array is a special kind of data type that represents multiple values. In the above example, it represents 12 month names. Here, “month” is the array name. Please note that boolean, string, float, and integer each data type represents only one value; on the other hand, array represents multiply values.
How variable stores array elements?
If you think variable as a container, then, the following picture shows how a scalar type variable (a scalar type variable contains boolean, integer, float, or string type data) and array type variable contain values.
Commonly used terms in array
- Element: Element of an array is the items it contains. An array can contain one or more elements.
- Value: Each element contains one value.
- Key or index: Key or index of an array is a unique number or a string that is associated with each value of an element.
- Length: The length of an array is the number of elements it contains.
How to create an array:
There are 2 ways to create an array-
- Using array() function.
Syntax
$array_name = array(value1, value2, value3, …);
Example<?php $month=array(“January”, “February”, “March” , “April” , “May” , “June” , “July” , “August” , “September” , “October” , “November” , “December”);?>
Array created In the above way has automatic index numbers starts from 0 and increases by 1 for the subsequent values. So, the first value “January” has the index number 0, next one “February” has 1, and so on.
Manually created index number
There is a variation of the array() function when creating an array. In this way, index/key number can be mentioned when declaring the values.
Syntax
array_name = array(
key1 => value1,
key2 => value2,
—
)
Example<?php $month = array( 0 => “January”, 1 => “February”, 2 => “March”, );?>
Short array syntax
PHP 5.4 has introduced short array syntax. Now, array() can be written as [].If we write the previous example using [], it would be-
Syntax
array_name = [
key1 => value1,
key2 => value2,
—
);
Example<?php $month = [ 0 => “January”, 1 => “February”, 2 => “March”, ];?>
- Another way to create an array is using [] identifier.
Syntax
$array_name[] = value;
Example<?php $month[]=“January”; $month[]=”February”; $month[]=”March”; ?>
In the above example, January has index no. 0, February has 1 and March has 2.
Index/key can be assigned between []. Like this –
Syntax
$array_name[key] = value;
Example<?php $month[0]=“January”; $month[1]=”February”; $month[2]=”March”;[/php] ?></li> </ol> <div class="note-nl"> <img alt="" src="http://schoolsofweb.com/wp-content/uploads/notes.png" /> <div>If you arbitrarily assign array keys. PHP won’t fill up the missing elements. In the following example, PHP will leave $car[2] and when you try to add a new element, PHP won’t insert that element in the $car[2], instead, the key for the new element would be the next number of the existing highest key. Here, key for the new element “Rolse Royce” would be 4; <?php $car[0]=’Ferray’; $car[1]=’Jaguar’; $car[3]=’Mercedes’; $car[]=’Rolls Royce’; ?>