View associative array elements with foreach

Asked

Viewed 2,210 times

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.

2 answers

2


You can loop inside each other:

 $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) {
  foreach($informacoes as $chave => $valor) {
    echo $chave;
    echo ': ';
    echo $valor;
    echo '<br />';
  }
}
  • Thank you, jHertel, you helped a lot!

1

You have placed an associative array within another associative array, try to use this code:

<?php

    header('Content-type: text/html; charset=utf-8');

        $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 $usuario){
            echo "Nome : " . $usuario['nome'] . "<br>";
            echo "Idade :" . $usuario['idade'] . "<br>";
            echo "Profissao :" . $usuario['profissao'] . "<br>";
            echo "<br>";
        }
?>
  • Eric, thanks for the help, but this code works, I had already tested it. I want to know with associative array how it works, you know? I want to know how I can show the array key and data at the same time using foreach.

  • If I understand what you want and only do so: foreach ($usuarios as $key => $value) { echo "{$key} => {$value} "; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.