PHP问题显示(echo)JSON对象项

时间:2018-04-28 22:35:47

标签: php json

我有一个显示一些发布数据的PHP文件:

$data = file_get_contents('php://input');

echo json_encode($data);

以上回报:

{"name":"mark","item":"car"}

现在我想回复这个名字,所以我尝试了:

echo $data[0].name;

但那给了我Error: [Object].[Object]

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您需要首先解码您的JSON输入:

// $data is an input string    
$data = file_get_contents('php://input');

// convert input string to PHP array    
$data = json_decode(data, true);

// echo just the name
echo $data['name'];

// dump the whole parsed input
var_dump($data);