-3

How can I print/echo all properties/attributes values from all Country objects in this code? I thought of using 'foreach' but I don't know the sintaxis. The echo statement is in the last line of code. Thanks!

    <?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>";**

}  
GTWerber
  • 3
  • 2
  • 2
    Possible duplicate of [Looping through all the properties of object php](https://stackoverflow.com/questions/4976624/looping-through-all-the-properties-of-object-php) – catcon Jul 14 '19 at 22:11

1 Answers1

0
foreach ($countries as $country) {
    foreach ($country as $k => $v) {
        echo $k . ': ' . $v . '<br>'; // nombre: Argentina ...
    }
}  
DanKud
  • 221
  • 1
  • 4
  • Thanks DanKud! That worked like a charm. Is there any way to do the same but using just one 'foreach'? – GTWerber Jul 14 '19 at 22:32