Lecture 5 - Arrays
Lecture 5 - Arrays
OUTLINE
1. Definition
2. Predefined arrays
3. Array declaration
4. Array printing
5. Multidimensional arrays
6. Q & A
ARRAYS
Hold multiple values
Structured as a series of key-value pairs
- $states['MD’] returns
Maryland
Others
$_ENV
$_SESSION
$_COOKIE
handle_form (lecture4).php
<?php
$name = $_REQUEST['name'];
$age = $_REQUEST['age'];
$gender=$_REQUEST['gender'];
….. handle_form (lecture5).php
?> <?php
$name = $_POST['name'];
$age = $_POST ['age'];
$gender=$_POST ['gender'];
…..
?>
ARRAYS
Creating arrays:
A. Add an element at a time :
$band[ ] = 'Jemaine';
$band[ ] = 'Bret'; ! $and[0] will be Jemaine, etc.
$band[ ] = 'Murray’;
$six $let
E. Use range function
Creates an array of sequential numbers or letters
arrays_1.php
ADDING ELEMENTS INTO
AN ARRAY
You can add a new item into an array by simply using this syntax:
$array_name[]=‘value’;
This statement will simply append that item to the end of the array.
You can add a new entry (key, value) to an associative arrays using this
syntax:
$associative_array_name[‘Key’]=‘value’;
If the key exists, it will simply update the value for the entry. Otherwise, it will
add a new entry.
ADDING ELEMENTS INTO
AN ARRAY
<?php
$fruits=array('Apples','Bananas','Dates','Oranges');
$fruits_2=array('red'=>'Apples','yellow'=>'Bananas',
'beige'=>'Cantaloupes','brown'=>'Dates');
adding_items_to_array.php
DELETING ELEMENTS
FROM AN ARRAY
You can use unset to delete an item from an array.
Once you delete the item, the item will seize to exist, and the
size of the array will shrink by 1.
<?php
$fruits=array('Apples','Bananas','Dates','Oranges');
$fruits_2=array('red'=>'Apples','yellow'=>'Bananas',
'beige'=>'Cantaloupes','brown'=>'Dates');
?>
removing_items_from_arrays.php
ITERATING THROUGH ARRAYS
Accessing an entire array
We have seen how to access individual elements using key
o Require a prior knowledge of the key
To access all elements, use foreach loop The loop will iterate through
foreach ($array as $value) { every element in $array,
assigning each element’s
// Do something with $value. value to the $value variable
}
<?php
$fruits=array('Apples','Bananas','Dates','Oranges');
/*iterating through an array */
foreach($fruits as $value) {
echo $value;
}?>
looping_through_an_array.php
ITERATING THROUGH ASSOCIATIVE
ARRAYS
To access both the keys and values, use
foreach ($array as $key => $value) {
echo "The value at $key is $value.";
}
<?php
$fruits=array('red'=>'Apples','yellow'=>'Bananas',
'beige'=>'Cantaloupes','brown'=>'Dates');
Brown ‘Dates’
ARRAYS
Example
arrays.php
<?php
// Make the months array:
$months = array ('January’, 'February', 'March', 'April’, 'May’,
'June', 'July', 'August', 'September’, 'October’,
'November', 'December’);
$num = count($array);
Is_array(): checks if the variable is of type array
explode()
Takes a string containing several items separated by a single character (or a
string of characters) and place each of these items into an array containing all
its words.
Example:
$temp=explode(' ', “This is a sentence”);
The value of $temp is:
Array( [0]==> This
[1]==>is
[2]==> a
[3]==> sentence
) array_functions.php
ARRAY FUNCTION
Function Description Example
calendar.php
CORRECT THE PATTERNS
QUESTION LAB-4
ANY QUESTIONS?
27
REFERENCES
• PHP
• https://www.w3schools.com/php/php_arrays.asp
• https://www.w3schools.com/php/php_arrays_multi.asp
• http://php.net/manual/en/langref.php
28