-1

Why am I getting undefined. That is most silly code I've posted but I have no PHP programmer to ask, so here I am.

 $resultados = ["nome" => "magalu", "idade" => 200];



 echo $resultados[0]["idade"];

Let's go

 $resultados = ["nome" => "diego", "idade" => 200];

I am creating a variable with index 0 equal to an array that has two keys "nome", "idade".

echo $resultados[0]["idade"];

Now I am accessing that index and displaying the value of the "idade" key.

Diego Alves
  • 2,462
  • 3
  • 32
  • 65

2 Answers2

0

What you should do is

echo $resultados["idade"];

It should be

$array[$key]

In your case keys are nome and idade

If you want to display the entire array you can use print_r or var_dump. For example print_r($resultados) or var_dump($resultados).

Areg
  • 1,414
  • 1
  • 19
  • 39
0

With what you have you have two indexes. 'nome' and 'idade'.

$resultados = ["nome" => "diego", "idade" => 200];

If you want 0 then you can add these to an array like this and then you can use 0 index.

$resultados = [["nome" => "diego", "idade" => 200]];
echo $resultados[0]["idade"];
blupointmedia
  • 564
  • 2
  • 10