Transform json array into php array

Asked

Viewed 9,622 times

1

Well, I have a little problem that I haven’t been able to solve. I already Googled about it, and the solutions I found did not suit my problem.

I need to transform the array "Players" that is in this JSON: http://mcapi.ca/query/ip.craftlite.com.br:25571/list in a PHP array.

{
  "Status": true,
  "Players": {
    "online": 8,
    "max": 60,
    "list":["Uchoa","zMarvado","Diego_Dias_BR_RJ","Konotero","Zirak_","Grafite07","MateusInox","Stevegamer138"]
  },
  "Version": "1.8.8"
}

This is the code I’m using, suggested by the answers below:

<?php

$lista = file_get_contents("http://mcapi.ca/query/ip.craftlite.com.br:25571/list");

$infor = json_decode($lista);

$players = $infor['Players'];
$players_list = $players['list'];

foreach($players_list as $i){
  echo $i;
}

?>

The error is in the variable $players, according to the PHP:

Fatal error: Cannot use object of type stdClass as array in /customers/6/8/7/craftlite.com.br/httpd.www/test.php on line 8 

I don’t know much about PHP, I’m more used to Java, so I don’t know how to identify the errors here.

  • 1

    json_decode with true returns an array, so you should not access the list property but something like $players = $Infor['Players']['list']. If this does not resolve put there the result of var_dump($Infor)

  • The dump is this: array(3) { ["Status"]=> bool(true) ["Players"]=> array(3) { ["online"]=> int(7) ["max"]=> int(60) ["list"]=> array(7) { [0]=> string(5) "Uchoa" [1]=> string(10) "Mateusinox" [2]=> string(8) "zMarvado" [3]=> string(8) "fullsets" [4]=> string(13) "Stevegamer138" [5]=> string(16) "Diego_dias_br_rJ" [6]=> string(8) "Konotero" } } ["Version"]=> string(5) "1.8.8" }

  • Brunno, never just put the link to something essential in your question, because if it gets lost, it will not be possible to help other people too, thus avoiding the purpose of the forum!!! =)

  • Thanks for the tip, unfortunately I already modified the test.php page, but next I put the error in the topic itself

6 answers

4


When you try to run

$infor->Players->list;

you are trying to access a Objeto.

But how are you reading one Objeto Json, you should read it this way:

$players = $infor["Players"];
$players_list = $players["list"]

And read the list:

foreach($players_list as $i){
    echo $i;
}
  • Oops, I’ll increment the code with this, mark solved if you help me!

  • A new error has arisen, I edited the question!

  • The mistake was mine panguice, now it is working well!

1

In order to access this way:

$infor->Players->list;

you need to remove the second parameter true:

$infor = json_decode($lista);

Otherwise, it will convert to an associative array, and will have to access it like this:

$players = $infor["Players"]["list"];

1

If in the json_decode function the true parameter is passed, json_decode returns an associative array.

$lista = file_get_contents("http://mcapi.ca/query/ip.craftlite.com.br:25571/list");
$infor = json_decode($lista,true);
$players = $infor['Players']['list'];

foreach ($players as $player) {
     echo ' '.$player.',';
}

If the json_decode function does not pass the true parameter, json_decode returns an Object.

$lista = file_get_contents("http://mcapi.ca/query/ip.craftlite.com.br:25571/list");
$infor = json_decode($lista);  
$players = $infor->Players->list;

foreach ($players as $player) {
    echo ' '.$player.',';
}

Reference php.net

0

I saw you didn’t put the ; at the end of file_get_contents.

<?php
$lista = file_get_contents("http://mcapi.ca/query/ip.craftlite.com.br:25571/list") <-
$infor = json_decode($lista, true);
$players = $infor->Players->list;
echo = "$players";
?>
  • Really rsrs, I realized this a few minutes ago

  • Worked now?

  • No, that wasn’t the problem. but now it’s solved

0

The mistake I had was solved as follows:

I followed in the footsteps of Hugo Fernandes and of Kenny Rafael, but I made a mess and ended up making another mistake. Let’s go in parts, aiming to help others who will use this question to help in their problems:

Where was my mistake? At first, the code was like this:

<?php
$lista = file_get_contents("http://mcapi.ca/query/ip.craftlite.com.br:25571/list")
$infor = json_decode($lista, true);
$players = $infor->Players->list;
echo = "$players";
?>

You can already notice a number of errors in the syntax, as the lack of ; after the attribute of $lista, and the = after the echo (echo is not a variable, so you can’t put value in echo).

Okay, by rewriting the code, it gets more straight:

<?php
$lista = file_get_contents("http://mcapi.ca/query/ip.craftlite.com.br:25571/list"); //Botei o ;
$infor = json_decode($lista, true);
$players = $infor->Players->list;
echo "$players";//Tirei o = 
?>

From there, we already noticed the gross error:

As said the Hugo Fernandes, I’m trying to access a Objeto, and my goal was to access a list of Objetos (One Array).

All right, I did what he suggested in his reply, so the code went like this:

<?php

$lista = file_get_contents("http://mcapi.ca/query/ip.craftlite.com.br:25571/list");

$infor = json_decode($lista);

$players = $infor['Players'];
$players_list = $players['list'];

foreach($players_list as $i){
   echo ' '.$i.',';
}

?>

But then the confusion arose. I read the answers "running" and I ended up pooping. o Kenny Rafael said I should access without the parameter true to access the Objeto, and then came the second error: I tried to access a JSON``Array from a JSON``Object, and ended up making the mistake:

Fatal error: Cannot use object of type stdClass as array in /customers/6/8/7/craftlite.com.br/httpd.www/test.php on line 8

I spent some time reading the answers and managed to orient myself. I called myself that I should put the parameter true in the json_decode, transforming the variable $infor in a JSON Array, instead of a JSON Object, then the code went like this:

<?php

$lista = file_get_contents("http://mcapi.ca/query/ip.craftlite.com.br:25571/list");

$infor = json_decode($lista, true);

$players = $infor['Players'];
$players_list = $players['list'];

foreach($players_list as $i){
  echo ' '.$i.',';
}

?> 

After the help of these two masters, and a little more attention, I completed the code without errors!

0

Do this test here that you will understand :D

$json_url = "http://mcapi.ca/query/ip.craftlite.com.br:25571/list";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);

echo "<pre>";
print_r($data);
echo "</pre>";

Browser other questions tagged

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