Reputation: 3
I'm trying to grab specific data using this api (for a Wordpress blog):
https://api.chess.com/pub/player/erik/stats
I'm lost with arrays and am unable to isolate specific data: for example, if I want "Last" "rating" for "chess_daily". Here is the code I use, but can't figure out how to target data.
<?php $request = wp_remote_get( 'https://api.chess.com/pub/player/erik/stats' );
if( is_wp_error( $request ) ) {
return false;
}
$body = wp_remote_retrieve_body( $request);
$test = json_decode( $body,true );
print_r ($test);
?>
Upvotes: 0
Views: 630
Reputation: 55427
PHP 7 added the null coalescing operator which is a great way to get data from array. For your sample you can run:
$last_chess_daily = $test['chess_daily']['last'] ?? null;
Which returns:
array (
'rating' => 1604,
'date' => 1616714557,
'rd' => 51,
)
Upvotes: 1