Transform json array into php array

0

Well, I have a little problem that I still can not solve. I already looked it up on Google, and the solutions I found did not serve my problem.

I need to transform the array "Players" that is in this JSON: link in one 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 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 do not understand much of PHP, I'm more used to Java, so I can not identify the errors here.

    
asked by anonymous 04.10.2016 / 21:28

6 answers

3

When you try to run

$infor->Players->list;

You're trying to access a Objeto .

But since you're reading a Objeto Json , you should read as follows:

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

And read the list:

foreach($players_list as $i){
    echo $i;
}
    
04.10.2016 / 21:40
1

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 you will have to access it like this:

$players = $infor["Players"]["list"];
    
04.10.2016 / 21:47
0

I saw that you did not put ; 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";
?>
    
04.10.2016 / 21:51
0

The error I had was resolved as follows:

I followed the steps of Hugo Fernandes and Kenny Rafael , however I made confusion and ended up giving another error. We are going in parts to help others who will use this question to help with their problems:

Where was my mistake? At first, the code looked 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 syntax errors, such as the lack of ; after the $lista attribute, and = after echo (echo is not a variable, so do not put value not echo).

Ok, reforming the code, it gets more accurate:

<?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 here, we've noticed the gross error:

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

Beauty, I did what he suggested in his response, so the code looks 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 came. I read the answers "running" and ended up doing poop. Kenny Rafael said that I should access without the true parameter to access Objeto , and then the second error appeared: I tried to access a JSON''Array from a JSON''Object , and ended up giving the error :

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 get my bearings. I 've called that you should set the parameter true to json_decode , turning the $infor variable into a JSON Array , instead of a JSON Object , then the code looks 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!

    
04.10.2016 / 22:32
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>";
    
04.10.2016 / 22:45
-1

If json_decode is passed in the json_decode function, 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.',';
}

php.net Reference

    
04.10.2016 / 22:02