Introduction to PHP Arrays
- Demonstrate two different ways to access an Item in an array
- Demonstrate modifying Array elements
- Demonstrate removing Array elements
- Two ways to access array
Example on accessing Elements in array:
$myFriends = array("Sam", "George", "Harry");
echo $myFriends[2];
echo $myFriends{2};
Example to modify an element in an array
$myFriends = array("Sam", "George", "Harry");
$myFriends[1] = "Mike";
echo $myFriends[1];
Example to remove an element in an array
$myFriends = array("Sam", "George", "Harry");
unset($myFriends[1])
foreach($myFriends as $friend){
echo "<p>$friend</p>";
}
- Create array of friends with 4 people in it ("Sam", "Mike", "George", "Harry")
- Echo out the third Element "George"
- Modify second element "Mike" to "Albert" and echo out
- Remove the 3rd element "George" (Use a foreach loop to display the elements in the array)
- Create an array with 10 elements of fruit (Apple, Banana, Cherry, Date, Elderberry, Fig, Grape, Honeydew, ilama, jujube)
- Echo out the 6th element "Fig".
- Modify the 3th element (Cherry) to Cranberry.
- Remove the 5th element (Elderberry) (Use a foreach loop to display the elements in the array)