1
Hello, everyone. I would like to display the elements of an associative array as follows:
Nome: José
Idade: 32
Profissão: Médico
Nome: Rafaela
Idade: 28
Profissão: Dentista
All on the same page. And the code I’m using is the following:
<?php
$usuarios = array(
array("nome"=>"José", "idade"=>32, "profissao"=>"Médico"),
array("nome"=>"Rafaela", "idade"=>28, "profissao"=>"Dentista"),
array("nome"=>"Gabriela", "idade"=>18, "profissao"=>"Não tem")
);
foreach ($usuarios as $informacoes => $dados){
echo $informacoes . " : ";
echo $dados;
}
?>
But with this code, I get the following error:
0 :
Notice: Array to string conversion in /opt/lampp/htdocs/Development/PHP/Exercicio9/usuarios.php on line 10
Array1 :
Notice: Array to string conversion in /opt/lampp/htdocs/Development/PHP/Exercicio9/usuarios.php on line 10
Array2 :
Notice: Array to string conversion in /opt/lampp/htdocs/Development/PHP/Exercicio9/usuarios.php on line 10
Array
Searching, I realized that to show the array would need to give a print_r
instead of echo
, but this command literally shows the array.
I wonder if it is possible and how I can display the indexes and the respective array values in the way I have shown.
From now on, thank you.
Thank you, jHertel, you helped a lot!
– boomboxarcade