2

I am obtaining a json object using the following:

$json = file_get_contents("url-here");
$data = json_decode($json, true);
//test
var_dump($data);

This gives me something like this:

array(2) { ["ok"]=> bool(true) ["result"]=> array(1) { [0]=> array(2) { ["update_id"]=> int(44893465) ["message"]=> array(5) { ["message_id"]=> int(16) ["from"]=> array(3) { ["id"]=> int(29595794) ["first_name"]=> string(3) "Bob" ["username"]=> string(14) "Bobo" } ["chat"]=> array(3) { ["id"]=> int(29595794) ["first_name"]=> string(3) "Bob" ["username"]=> string(14) "Bobo" } ["date"]=> int(1435354253) ["text"]=> string(7) "/q 3.33" } } } }

I would then like to add certain values into variables. For example I would like to extract username, text, message_id, etc

But no matter what I try my variables are empty:

//let's test it
echo "Username: " . $data[1][0]["username"];

//another test
echo $data->username;

Neither of these are working and my research has not helped me find a solution. I am stumped on this one.

Any pointers in the right direction would really be appreciated.

BrokenCode
  • 951
  • 4
  • 19
  • 43

2 Answers2

1
 array(2) {

        ["ok"]=> bool(true) 
        ["result"]=> array(1) 
        { 
            [0]=> array(2) 
                { 
                    ["update_id"]=> int(44893465) 
                    ["message"]=> array(5) 
                        { 
                            ["message_id"]=> int(16) 
                            ["from"]=> array(3) 
                            { 
                                ["id"]=> int(29595794) 
                                ["first_name"]=> string(3) "Bob" 
                                ["username"]=> string(14) "Bobo" 
                            } 
                            ["chat"]=> array(3) 
                            { 
                                ["id"]=> int(29595794) 
                                ["first_name"]=> string(3) "Bob" 
                                ["username"]=> string(14) "Bobo" 
                            } 
                            ["date"]=> int(1435354253) 
                            ["text"]=> string(7) "/q 3.33" 
                        } 
                } 
        } 
    }

You are using wrong array index. $data[1][0]["username"]; not exists.

$data["result"][0]["message"]["from"]["username"]; 
$data["result"][0]["message"]["chat"]["username"]; 

This will give you the proper username

Tintu C Raju
  • 2,700
  • 2
  • 21
  • 25
  • Thanks so much. Your representation of the hierarchy of the array helped me very much. Is there a method in PHP to print something like that? I tried print_r but it's not that helpful. – BrokenCode Jun 27 '15 at 10:56
  • `var_dump` is the best way to display array in such a manner. i dont know why do you get such a result with `var_dump`. try to display in `
    ` tags.   http://stackoverflow.com/questions/5393085/display-an-array-in-a-readable-hierarchical-format
    – Tintu C Raju Jun 27 '15 at 11:09
0
$json = file_get_contents("url-here");
$data = json_decode($json, true);
//test

echo $data["result"][0]['message']['from']['username'];

output Bobo

Double H
  • 4,120
  • 1
  • 15
  • 26