Chapter 4 Arrays in PHP
Chapter 4 Arrays in PHP
<?php
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
print_r($sliced);
?>
Output:
Array
(
[0] => banana
[1] => cherry
[2] => date
)
Converting between Arrays
and Variables
• PHP provides two functions extract() and
compact() that convert between arrays and
variables.
• extract(): extract() function
automatically creates local variables from
an array.
• compact():The compact() function creates
an array from variables and their values.
Traversing Arrays
• foreach loop
• for loop
• The iterator functions: Every PHP array keep track of the current element. The pointer to
the current element is known as the iterator.
• Iterator functions are :
1. current(): Returns the currently pointed element.
2. reset(): Moves the iterator to the first element in the array and returns it.
3.next():Moves the iterator to the next element in the array and returns it.
4.prev():Moves the iterator to the previous element in the array and returns it.
5.end():Moves the iterator to the last element in the array and returns it.
Conclusion