2
I have that code PHP
that is manipulating the JSON
created in the beginning, in the end it will print/list one foreach
with all the data.
My question is the following, I don’t want to print all 3 lines, I want to print only the second line of my JSON
for example.
How could I do that?
<?php
$json_str = '{"empregados": '.
'[{"nome":"Jason Jones", "idade":38, "sexo": "M"},'.
'{"nome":"Ada Pascalina", "idade":35, "sexo": "F"},'.
'{"nome":"Delphino da Silva", "idade":26, "sexo": "M"}'.
']}';
$jsonObj = json_decode($json_str);
$empregados = $jsonObj->empregados;
foreach ( $empregados as $e )
{
echo "nome: $e->nome - idade: $e->idade - sexo: $e->sexo<br>";
}
?>
print only line 2 (
$empregados[1]
) because thearray
in PHP has its start by 0 that being the position 1 and so on.– novic
$empregados
is an array, so to access the second line just do$empregados[1]
.– Woss