【发布时间】:2015-09-14 06:26:34
【问题描述】:
我正在使用以下方法获取 json 对象:
$json = file_get_contents("url-here");
$data = json_decode($json, true);
//test
var_dump($data);
这给了我这样的东西:
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" } ["聊天"]=> 数组(3) { ["id"]=> int(29595794) ["first_name"]=> 字符串(3) "鲍勃" ["用户名"]=> 字符串(14) "鲍勃" } ["日期"]=> int(1435354253) ["text"]=> string(7) "/q 3.33" } } } }
然后我想将某些值添加到变量中。例如我想提取用户名、文本、message_id 等
但无论我尝试什么,我的变量都是空的:
//let's test it
echo "Username: " . $data[1][0]["username"];
//another test
echo $data->username;
这些都不起作用,我的研究也没有帮助我找到解决方案。我被这个难住了。
任何指向正确方向的指针都将不胜感激。
【问题讨论】: