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 this on the same page. And the code I'm using is as follows:
<?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 it would be necessary to give print_r
instead of echo
, but this command literally shows the array.
I would like to know if it is possible and how can I do to display the indexes and their array values in the way I showed.
Thanks in advance.