简体   繁体   中英

Extract data from Array/Object in PHP?

First off, I'm new to PHP and coding in general, so this might be quite an obvious answer.

I'm currently working with the Strava API, and I'm trying to extract data from an Array/Object which is the result of the following API call:

$recentactivities = $api->get('athlete/activities', array('per_page' => 100));

which returns:

Array (
[1] => stdClass Object (
   [id] => XXXX
   [resource_state] => 2
   [external_id] => XXXX
   [upload_id] => XXXX
   [athlete] => stdClass Object (
      [id] => XXXX
      [resource_state] => 1
   )
   [name] => Let\'s see if I can remember how to do this cycling malarkey...
   [distance] => 11858.3
   [moving_time] => 1812
   [elapsed_time] => 2220
   [total_elevation_gain] => 44
   [type] => Ride
   [start_date] => 2014-07-12T13:48:17Z
   [start_date_local] => 2014-07-12T14:48:17Z
   [timezone] => (
      GMT+00:00
   ) Europe/London
   [start_latlng] => Array (
      [0] => XXXX
      [1] => XXXX
   )
   [end_latlng] => Array (
      [0] => XXXX
      [1] => -XXXX
   )
   [location_city] => XXXX
   [location_state] => England
   [location_country] => United Kingdom
   [start_latitude] => XXXX
   [start_longitude] => XXXXX
   [achievement_count] => 4
   [kudos_count] => 1
   [comment_count] => 0
   [athlete_count] => 1
   [photo_count] => 0
   [map] => stdClass Object (
      [id] => a164894160
      [summary_polyline] => XXXX
      [resource_state] => 2
   )
   [trainer] =>
   [commute] =>
   [manual] =>
   [private] =>
   [flagged] =>
   [gear_id] => b739244
   [average_speed] => 6.544
   [max_speed] => 10.8
   [average_cadence] => 55.2
   [average_temp] => 29
   [average_watts] => 99.3
   [kilojoules] => 179.9
   [device_watts] =>
   [average_heartrate] => 191.2
   [max_heartrate] => 200
   [truncated] =>
   [has_kudoed] =>
   )

This repeats for the most recent activities.

I'm attempting to extract average_heartrate , which I can do for a single object using the following:

$recentactivities[1]->average_heartrate;

but I'd like to extract all instances of average_heartrate from the Array. I've tried to use a foreach statement, but to be honest, I have no idea where to start.

Any help would be much appreciated.

It's actually pretty simple you can indeed use a foreach loop:

foreach($myArray as $obj){
  //$obj is an object so:
  if(isset($obj->average_heartrate)){
     echo $obj->average_heartrate;
  }
}

With this code you iterate through your array with objects and save the wanted array within an array so you can work further with it.

$heartrateArray = array(); // create a new array

//iterate through it with an foreach

foreach($recentactivities as $activity){

    // save the average_heartrate as a value under a new key in the array
    $heartrateArray[] = $activity->average_heartrate;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM