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-
&amp;lt;!--?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”; ?--&amp;gt;
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 –
&amp;lt;!--?php $month=array(“January”, “February”, “March” , “April” , “May” , “June” , “July” , “August” , “September” , “October” , “November” , “December”); ?--&amp;gt;
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
&amp;lt;!--?php $month=array(“January”, “February”, “March” , “April” , “May” , “June” , “July” , “August” , “September” , “October” , “November” , “December”);?--&amp;gt;
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&amp;lt;!--?php $month = array( 0 =--&amp;gt; “January”, 1 =&amp;amp;gt; “February”, 2 =&amp;amp;gt; “March”, );?&amp;amp;gt;
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&amp;lt;!--?php $month = [ 0 =--&amp;gt; “January”, 1 =&amp;amp;gt; “February”, 2 =&amp;amp;gt; “March”, ];?&amp;amp;gt;
- Another way to create an array is using [] identifier.
Syntax
$array_name[] = value;
Example
&amp;lt;!--?php $month[]=“January”; $month[]=”February”; $month[]=”March”; ?--&amp;gt;
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
?>&amp;lt;!--?php $month[0]=“January”; $month[1]=”February”; $month[2]=”March”;
&amp;lt;!--?php $car[0]=’Ferray’; $car[1]=’Jaguar’; $car[3]=’Mercedes’; $car[]=’Rolls Royce’; ?--&amp;gt;
How to change values of an array:
To change an existing value of an element, you need to specify the new value in the array mentioning the key of that element in [] brackets.
Syntax
$array_name[existing_key] = “new_value”;
Example
&amp;lt;!--?php $month = array( 0 =--&amp;gt; “January”, 1 =&amp;amp;gt; “February”, 2 =&amp;amp;gt; “March”, ) array[2] = “December”; ?&amp;amp;gt;
How to delete an array:
To delete an array element or an entire array, unset() function can be used.
&amp;lt;!--?php $car = array(“Ferarry”, “Huyandai”, “Toyota”, “Roles Royace”); unset(car[2]); // Array element are now Ferarry, Huyandai, NULL, Roles Royace unset(car); // Array element are now NULL ?--&amp;gt;
Types of array
There are two types of array
- Indexed array
- Associative array
Indexed array
Array with numeric keys/index is called indexed array. This is the most common type of array. The arrays that we used so far are all indexed array.
Associative array
So far, you’ve seen number is used as key/index. Index can also be string too. This type of array is associative array.
&amp;lt;!--?php $January = array( “position” =--&amp;gt; “1st”, &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; “days” =&amp;amp;gt; 31, &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; “year” =&amp;amp;gt;2013, &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; ) ?&amp;amp;gt;
Multidimensional array
When a key of an array represents another array it is called multidimensional array. In all the array examples above each key represents one value. In this sense, those are one-dimensional arrays.
&amp;lt;!--?php $friends = array( array(“John”, 20, “US”), array(“Jack”, 25, “CA”), ) ?--&amp;gt;
How to access an array element?
To print “January” from the array “month” above, you’ll write –
Syntax
echo $array_name[key];
Example
&amp;lt;!--?php echo $month[0]; ?--&amp;gt;
Output: January
If key is not assigned, array key starts from 0, so, the first element stores January.
Next Lesson: PHP Operators – Part1 ›› |