user9273032
user9273032

Reputation:

PHP Issue displaying (echo) JSON object items

I have a PHP file which is displaying some posted data:

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

echo json_encode($data);

The above returns:

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

Now I want to echo just the name so I tried:

echo $data[0].name;

But that's giving me Error: [Object].[Object]

How can I fix this?

Upvotes: 0

Views: 163

Answers (1)

krlv
krlv

Reputation: 2380

You need to decode your JSON input first:

// $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);

Upvotes: 1

Related Questions