¿Cómo analizo JSON con PHP?

De acuerdo con su código JSON, el siguiente código debería funcionar:

$response = file_get_contents($uri); $json = json_decode($response); foreach ($json->data as $beer) { echo $beer->name."\n"; } 

Si quieres saber cuántas cervezas hay, usa count($json->data) .

Con varias páginas, el código puede ser el siguiente:

 // here we store the beers names $beer_names = array(); // the current page $page = 0; // the maximum number of pages // (we don't know for the moment, so // we set it to 0) $max_pages = 0; do { $page++; // get the data for page $page $response = file_get_contents($uri.'&p='.$page); // decode the data $json = json_decode($response); foreach ($json->data as $beer) { $beer_names []= $beer->name: } // if $max_pages is 0 (ie is not set), we set // it with the given value if ($max_pages == 0) { $max_pages = $json->numberOfPages; } } while ($page < $max_pages); echo implode("\n", $beer_names); 

Haga una var_dump en $ myBeer.

Verá que todas las partes envueltas en {} no son StdClass y todas las partes envueltas en [] son ​​matrices.

Esto significa que para obtener la ‘página actual’ llamaría $ myBeer-> currentPage. Para obtener el primer elemento en la matriz de datos, debe llamar a $ myBeer-> data [0].

Discúlpeme por la respuesta tarde. Intenta algo como esto

 foreach ( $myBeer->trends as $trend ) { echo "{$trend->name}\n"; } 

De ( http://stackoverflow.com/questio …)