如何打印/回显/获取此代码中每个对象的所有属性?

如何打印/回显此代码中所有 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>";**


}  


鸿蒙传说
浏览 159回答 1
1回答

陪伴而非守候

foreach ($countries as $country) {&nbsp; &nbsp; foreach ($country as $k => $v) {&nbsp; &nbsp; &nbsp; &nbsp; echo $k . ': ' . $v . '<br>'; // nombre: Argentina ...&nbsp; &nbsp; }}&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP