0

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?

3
  • $data->name 15 chars in length for a comment. fgot Commented Apr 28, 2018 at 22:37
  • Sorry but that's returning Null Commented Apr 28, 2018 at 22:40
  • echo $data['name'] will outputs mark Commented Apr 28, 2018 at 22:42

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.