Transfer keys and values from a JSON to PHP variables

Asked

Viewed 1,217 times

3

How to transfer data from a JSON:

{"resultado":true,"cadastros":[{"id":"12345","nome":"Augusto","idade":"30"},{"id":"23411","nome":"Carlos","idade":"93"},{"id":"13451","nome":"Bruno","idade":"23"}],"mensagem":"Success"}

For variables in PHP resulting in:

`$id[0] = "12345"; $nome[0] = "Augusto"; $idade[0] = "30"; $id[1] = "23411"; $nome[1] = "Carlos"; $idade[1] = "93";`

and also the amount of records, which in total of this example are 3.

$registros = "3";

I tried using json_decode() along with foreach() but it didn’t work. I don’t know how to develop the logic.

<?php   
    $json = '{"resultado":true,"cadastros":[{"id":"12345","nome":"Augusto","idade":"30"},{"i‌​d":"23411","nome":"Carlos","idade":"93"},{"id":"13451","nome":"Bruno","idade":"23‌​"}],"mensagem":"Success"}'; 
    $a = json_decode($json, true); 
    echo $a;   
?> 

results in

Notice: Array to string Conversion in C: Program Files Easyphp-Devserver-14.1VC11 data localweb teste.php on line 4

Array line 4 is the echo $a;

  • What is the result of json_decode()?

  • <?php&#xA;$json = '{"resultado":true,"cadastros":[{"id":"12345","nome":"Augusto","idade":"30"},{"id":"23411","nome":"Carlos","idade":"93"},{"id":"13451","nome":"Bruno","idade":"23"}],"mensagem":"Success"}';&#xA;$a = json_decode($json, true);&#xA;echo $a;&#xA;?> results in Notice: Array to string conversion in C:\Program Files\EasyPHP-DevServer-14.1VC11\data\localweb\teste.php on line 4&#xA;Array line 4 is the echo $a;

  • I inserted your comment into the question. You can always edit the question to add details. Now that the comment is in question you can delete the comment here. I will also delete it when you see it. Welcome!

  • What appears when you test var_dump($a);, before the echo ?

  • I was able to solve 90% of the problem with Filipe’s solution: $a_Dados = json_decode($json, true);&#xA;echo $a_Dados["resultado"]."<br>"; //true&#xA;echo $a_Dados["cadastros"][1]["nome"]; //12345 Now missing to get the amount of records that are 3. How to proceed?

  • 1

    @user3486019: testing count($a_Dados["cadastros"]);

Show 1 more comment

1 answer

1


Utilize json_decode with the second parameter "true", an associative array will be returned, i.e., items formed by a key pair and value, in which each key has an associated value.

An example, where $url is the json link.

$a_Dados = json_decode(file_get_contents($url), true);
echo $a_Dados["resultado"]; //true
echo $a_Dados["cadastros"][0]["id"]; //12345

If necessary, make one:

$a_Dados = json_decode(file_get_contents($url), true);
for($i=0; $i<count($a_Dados["cadastros"]); $i++){
    echo $a_Dados["cadastros"][$i]["id"];
}

If you have a json encoded variable, change the following line:

 $a_Dados = json_decode($variavel_codifica_json,true);
  • You solved 90% of my problem. Have some practical way to get the amount of registrations?

  • Closed. Thank you.

  • @user3486019 yes, just do it $total = Count($a_Dados["entries"]); $total will be equal to number of entries, or then see the second part I posted, has a "for" on top of the key "entries".

Browser other questions tagged

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