I have a similar question to Get values stdClass Object PHP however, I'm kinda lost in the answer of what was the object is he referring to.
Here's the code, and wanting to get the value of avg_open_rate and avg_click_rate from $statsInfo[] and display it to my output text copy.
if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
foreach ( $body->members as $member ) {
if( $member->unique_email_id != '4b6354db09' )
continue;
$user = $member->email_address;
$statsInfo[] = $member->stats;
}
print_r( $statsInfo );
echo '<p> User: '. $user .' </p>';
echo '<p> Click Rate: '. $statsInfo->avg_click_rate .' </p>';
echo '<p> Open Rate: '. $statsInfo->avg_open_rate .' </p>';
} else {
echo '<b>' . wp_remote_retrieve_response_code( $response ) . wp_remote_retrieve_response_message( $response ) . ':</b> ' . $body->detail;
}
Ouput on print_r and echo:
Array (
[0] => stdClass Object (
[avg_open_rate] => 0
[avg_click_rate] => 0
)
)
User: [email protected]
Click Rate:
Open Rate:
$statsInfois an array of stdObject, so you have to use array index to access the stdObject first:$statsInfo[0]->avg_open_rate,$statsInfo[0]->avg_click_rateforeachloop?$usereach time through the loop. At the end it will be the last user, not all the users whose stats you collected.