0

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:
6
  • 2
    Your $statsInfo is 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_rate Commented Oct 31, 2019 at 0:08
  • ok, i figured out what was I missed there.. thanks for the suggestion. Commented Oct 31, 2019 at 0:11
  • Do you understand what @catcon is telling you? Commented Oct 31, 2019 at 0:21
  • Why don't you just print the stats inside the foreach loop? Commented Oct 31, 2019 at 0:21
  • You're overwriting $user each time through the loop. At the end it will be the last user, not all the users whose stats you collected. Commented Oct 31, 2019 at 0:23

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.