0

Below is a sample json code that I want to access...This json is actually coming from an API. Whenever I copy and paste the entire json in json lint..It says valid json..

foreach ($movies as $movie) {
echo "theaters ".var_dump($movie->release_dates->theater)";
}

//Im actually trying to access a nested json in php... Something like
{
"movies":[{
"tile":"Cowboys";
"release_dates":{"theater":"2013-11-29"}, 
so on....

Whenever I try to write the above it gives me an error Object of stdclass cannot be converted to string ....Or if I write

$x = json_decode(var_dump($movie->release_dates->theater), true)";
echo "theaters "$x[0];

It gives an output like string[10]:2013-11-25 string[10]:2013-11-30....So on..What is the error....

Ayush
  • 41,754
  • 51
  • 164
  • 239
user2779848
  • 39
  • 2
  • 9
  • 4
    `var_dump` doesn't return anything, so it's not clear what you're trying to do. – Barmar Oct 31 '13 at 18:12
  • To expand on @Barmar's point, `var_dump` is for outputting debug info to yourself, not parsing JSON. – Seventoes Oct 31 '13 at 18:33
  • Actually I want to print tile, release_dates where this json response is coming from an API.. I have used curl to send the request and response..Im able to print tile by looping through as in the above code like foreach($movies as $ movie){ echo"$movie->tile";} This is printing the response successfully.But I am unable to retreive the date for the release_dates in the json..How do I do it?? – user2779848 Oct 31 '13 at 19:07
  • Please post the output of `var_dump($movie)` – Barmar Oct 31 '13 at 19:12
  • It gives an output of everything..All the unformatted data in the page when I say var_dump($movie)...Like object(stdClass)#2(12){["title"]=>string(24) so on – user2779848 Nov 01 '13 at 01:27
  • To make things simpler...This is the code I have.. – user2779848 Nov 01 '13 at 01:42

1 Answers1

0
$data = json_decode($movies, true);

foreach ($data as $movie) {
   echo "theaters ".implode(', ', $movie->release_dates->theater)";
}
halfik
  • 34
  • 2