I've seen lot's of answers to similar questions, but none that I can parse with limited PHP chops. Simply, I want to create a quick list of objects, then loop through that array and access the properties of each object. In javascript it's very simple:
var arrayOfObjects = [
{'color': 'red', 'size': 10},
{'color': 'blue', 'size': 4},
{'color': 'green', 'size': 6}
];
for(var i=0; i<arrayOfObjects.length; i++) {
console.log(arrayOfObjects[i]['color']);
console.log(arrayOfObjects[i]['size']);
}
I'm using PHP 5.3, and here's my first ugly stab at it:
$array_of_objects = array();
$object = new stdClass();
$object->color = 'red';
$object->size = 10;
array_push($array_of_objects, $object);
$object2 = new stdClass();
$object2->color = 'green';
$object2->size = 4;
array_push($array_of_objects, $object2);
This looks terrible.
$objects = array( (object)array('color'=>'red', 'size'=>'none'), (object)array('color'=>'blue', 'size'=>100) );. Just readed inside the php-doc-comments.