note 18329 deleted from control-structures.foreach by didou
| From: | didou@php.net | Date: | Sun, 05 Oct 2003 16:36:18 +0000 |
| Subject: | note 18329 deleted from control-structures.foreach by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57845@lists.php.net to get a copy of this message | ||
Note Submitter: richardNO@SPAMPLEASEcwndesign.co.uk
----
I've just discovered that 'foreach' allows you to loop through / enumerate the
properties of (copies of) objects as well as arrays...
<?php
class person
{
var $firstName = "Joe";
var $lastName = "Bloggs";
var $age = "25";
}
$person = new person;
foreach ($person as $key=>$val)
{
echo "$key = $val\n";
}
/*
Prints...
firstName = Joe
lastName = Bloggs
age = 25
*/
?>
...why didn't anyone tell me about this before? :)