0

So, I have this api from this website (not my website and I don't have access to the files)

Here's what the api's response looks like:

{
  "previousPageCursor": null,
  "nextPageCursor": null,
  "data": [
    {
      "userAssetId": 413511366,
      "serialNumber": null,
      "assetId": 1028606,
      "name": "Red Baseball Cap",
      "recentAveragePrice": 271,
      "originalPrice": null,
      "assetStock": null,
      "buildersClubMembershipType": 0
    },
    {
      "userAssetId": 1075234337,
      "serialNumber": 820,
      "assetId": 93131552,
      "name": "Dark Conjurer",
      "recentAveragePrice": 59910,
      "originalPrice": 250,
      "assetStock": 900,
      "buildersClubMembershipType": 0
    },
    {
      "userAssetId": 4506314814,
      "serialNumber": 944,
      "assetId": 439946101,
      "name": "Adurite King of the Night ",
      "recentAveragePrice": 9805,
      "originalPrice": 100,
      "assetStock": 2000,
      "buildersClubMembershipType": 0
    },
    {
      "userAssetId": 4779382222,
      "serialNumber": null,
      "assetId": 71484026,
      "name": "Sinister Branches",
      "recentAveragePrice": 2223,
      "originalPrice": 125,
      "assetStock": null,
      "buildersClubMembershipType": 0
    },
    {
      "userAssetId": 20445168513,
      "serialNumber": 4667,
      "assetId": 1609401184,
      "name": "Supa Dupa Fly Cap",
      "recentAveragePrice": 5488,
      "originalPrice": 1000,
      "assetStock": 10000,
      "buildersClubMembershipType": 0
    }
  ]
}

I want to grab all the "recentAveragePrice" values from the api then add the values up in php but I can't figure it out.

Anas
  • 971
  • 13
  • 28
Sani
  • 11
  • 2
  • 1
    So what you have done so far in code to achieve this? – Mayank Pandeyz Jul 27 '18 at 05:33
  • @MayankPandeyz I'm a php newbie, I only know the basics, I don't know how to grab more than one of the same value is the problem – Sani Jul 27 '18 at 05:36
  • @Sani just to give answerers an easier time, consider replacing that single line JSON response with a prettified version. One way to generate a prettier version of JSON is to use `jq` (`jq . /path/to/json` should work). – ctt Jul 27 '18 at 05:40
  • How you are calling this API ? – Mayank Pandeyz Jul 27 '18 at 05:42
  • Get this response in a variable and convert it into an array by using `json_decode()` and use it accordingly. – Mayank Pandeyz Jul 27 '18 at 05:42

1 Answers1

0

Try this:

$responseJson = '{"previousPageCursor":null,"nextPageCursor":null,"data":[{"userAssetId":413511366,"serialNumber":null,"assetId":1028606,"name":"Red Baseball Cap","recentAveragePrice":271,"originalPrice":null,"assetStock":null,"buildersClubMembershipType":0},{"userAssetId":1075234337,"serialNumber":820,"assetId":93131552,"name":"Dark Conjurer","recentAveragePrice":59910,"originalPrice":250,"assetStock":900,"buildersClubMembershipType":0},{"userAssetId":4506314814,"serialNumber":944,"assetId":439946101,"name":"Adurite King of the Night ","recentAveragePrice":9805,"originalPrice":100,"assetStock":2000,"buildersClubMembershipType":0},{"userAssetId":4779382222,"serialNumber":null,"assetId":71484026,"name":"Sinister Branches","recentAveragePrice":2223,"originalPrice":125,"assetStock":null,"buildersClubMembershipType":0},{"userAssetId":20445168513,"serialNumber":4667,"assetId":1609401184,"name":"Supa Dupa Fly Cap","recentAveragePrice":5488,"originalPrice":1000,"assetStock":10000,"buildersClubMembershipType":0}]}';

$responseArr = json_decode($responseJson, true);
echo array_sum(array_column($responseArr['data'], 'recentAveragePrice'));

eval.in demo

This first uses json_decode to convert your JSON to a PHP array (the second parameter of true makes it an array instead of an object), then uses array_column to get the recentAveragePrice column of data, and finally uses array_sum to sum that array.

Ethan
  • 4,295
  • 4
  • 25
  • 44