-1

I have data getting from from an API. The result is an array that looks like :

{
  "values": [
    {
      "value": 1.50590809747133,
      "timestamp": 1558540800
    },
    {
      "timestamp": 1558548000,
      "value": 1.52303142144612
    },
    {
      "value": 1.49521152223328,
      "timestamp": 1558555200
    }
  ],
  "unit": "%"
}

How should I do to build another array composed of all the "value" items only(1.50, 1.52, 1.49)?

Any help would be very appreciated
Thanks !

Dharman
  • 30,962
  • 25
  • 85
  • 135
Julien
  • 45
  • 1
  • 3
  • 15

2 Answers2

0

I assume that you know to decode the JSON. Then just extract the column that you want, in this case value:

$array = json_decode($json, true);
$result = array_column($array['values'], 'value');
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
-1

From https://www.php.net/manual/en/function.array-values.php

$associative_array = json_decode($json, true);
$indexed_array = array_values($associative_array);

There you go.

Catar4
  • 198
  • 11