如何打印/回显此代码中所有 Country 对象的所有属性/属性值?我想使用'foreach',但我不知道sintaxis。echo 语句位于最后一行代码中。谢谢! <?php class Country { public $nombre; public $poblacion; public $idioma; public $moneda; public $economia; public $deporte;}$country1 = new Country ();$country1->nombre = 'Argentina';$country1->poblacion = '40 millones';$country1->idioma = 'Castellano';$country1->moneda = 'Peso argentino';$country1->economia = 'Agro';$country1->deporte = 'Futbol';$country2 = new Country ();$country2->nombre = 'USA';$country2->poblacion = '200 millones';$country2->idioma = 'Ingles';$country2->moneda = 'Dolar estadounidense';$country2->economia = 'Multiples industrias';$country2->deporte = 'Basket,Baseball,Futbol Americano';$country3 = new Country ();$country3->nombre = 'Alemania';$country3->poblacion = '50 millones';$country3->idioma = 'Aleman';$country3->moneda = 'Euro';$country3->economia = 'Multiples industrias';$country3->deporte = 'Futbol';$countries = [$country1,$country2,$country3];foreach ($countries as $country){ **echo $country->¿? "<br>";**}
1 回答
陪伴而非守候
TA贡献1757条经验 获得超8个赞
foreach ($countries as $country) {
foreach ($country as $k => $v) {
echo $k . ': ' . $v . '<br>'; // nombre: Argentina ...
}
}
- 1 回答
- 0 关注
- 158 浏览
添加回答
举报
0/150
提交
取消