我有一个代码,其中一个数组包含对象,其中包含对象,例如:
<?php
class person {
public $name;
public $foods=array();
}
class food {
public $foodnames=array() ;
}
$peoplearray[$name] = new person;
$peoplearray[$name]->name = 'john' ;
$peoplearray[$name]->foods[$key] = new food;
$peoplearray[$name]->foods[$key]->foodnames[$key] = 'ice cream' ;
$peoplearray[$name]->foods[$key] = new food;
$peoplearray[$name]->foods[$key]->foodnames[$key] = 'banana' ;
$peoplearray[$name] = new person;
$peoplearray[$name]->name = 'julie' ;
$peoplearray[$name]->foods[$key] = new food;
$peoplearray[$name]->foods[$key]->foodnames[$key] = 'chocolate' ;
$peoplearray[$name]->foods[$key] = new food;
$peoplearray[$name]->foods[$key]->foodnames[$key] = 'coffee' ;
$peoplearray[$name]->foods[$key] = new food;
$peoplearray[$name]->foods[$key]->foodnames[$key] = 'rice' ;
?>
现在我也需要遍历食物类中的所有对象,以便获取它们的属性。有谁知道这样做最有效的方法?