Page 193 - CITS - Computer Software Application -TT
P. 193
COMPUTER SOFTWARE APPLICATION - CITS
Keys and Values
The array above is an indexed array, where the first item has the key 0, the second has the key 1, and so on.
Associative arrays are different, associative arrays use named keys that you assign to them, and when looping
through associative arrays, you might want to keep the key as well as the value.
This can be done by specifying both the key and value in the foreach defintition, like this:
Example:
Print both the key and the value from the $members array:
The for each Loop on Objects
The for each loop can also be used to loop through properties of an object:
Example:
Print the property names and values of the $myCar object:
180
CITS : IT&ITES - Computer Software Application - Lesson 47 - 62