0

How can I retrieve the data from this array without using the timestamp?

e.g. element 1, element 25, element 45

{
    "1620066141": [21.5, 1117, 45, 52, 1010.5],
    "1620067941": [20.399999999999999, 738, 44, 53, 1010.4],
    "1620069741": [20.699999999999999, 961, 45, 54, 1010.2],
    "1620071541": [21.199999999999999, 1131, 45, 53, 1010.1],
[...]
}

Like:

$data->body->element[1]
$data->body->element[25]
$data->body->element[45]
GTT
  • 19
  • 4
  • 1
    Does this answer your question? [Get nth key of associative php array](https://stackoverflow.com/questions/29737606/get-nth-key-of-associative-php-array) – biesior May 09 '21 at 16:02

5 Answers5

0

let the old array be $old and new is $new

foreach($old as key => $value){
    $new[] = $value; 
}

i think also you want to add it to $data->body use:

$data->body->element = $new;
  • This is exactly what [`array_values`](https://www.php.net/manual/en/function.array-values.php) does. – El_Vanja May 09 '21 at 17:05
0

Use Foreach loop to access each node's data.

foreach($datas as $data) {
     echo $data;
}
0

You can use array_values(). However, array_values() only accepts a array. When you have an object, you need to convert it to an array.

$json = <<< DATA
{"1620066141": [21.5, 1117, 45, 52, 1010.5],
    "1620067941": [20.399999999999999, 738, 44, 53, 1010.4],
    "1620069741": [20.699999999999999, 961, 45, 54, 1010.2],
    "1620071541": [21.199999999999999, 1131, 45, 53, 1010.1]
}
DATA;
$data = json_decode($json);

$element = array_values((array)$data);

print_r($element[0]);
jspit
  • 7,276
  • 1
  • 9
  • 17
0

Object.keys() and map() method in ES6

Here is another possible way using the Object.keys() and map() method in ES6 to do it in one line:

let element = Object.keys(myObject).map(val => myObject[val]);

// let's declare your object {} as variable
const myObject = {
  "1620066141": [21.5, 1117, 45, 52, 1010.5],
  "1620067941": [20.399999999999999, 738, 44, 53, 1010.4],
  "1620069741": [20.699999999999999, 961, 45, 54, 1010.2],
  "1620071541": [21.199999999999999, 1131, 45, 53, 1010.1],
};

// Create an array of strings that represent all the enumerable
// properties of the given object 
// (here: 'timestamps' , which are your keys)
//
// Then:
// Map that array of properties (keys) into their values 
// (here: element[] -- the values without keys)

let element = Object.keys(myObject).map(val => myObject[val]);

console.log(element);

// now you can access your data via index
// element[0], element[1], etc ...

console.log('first element:')
console.log(element[0]);

console.log('third element:')
console.log(element[2]);

You can read more about Object.keys() method at:

MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

You can read more about map() method at:

MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Hope this adds some value.

Jürgen Fink
  • 3,162
  • 2
  • 23
  • 25
0

Thank you! My solution:

$data = json_decode($json, true);
$element = array_values($data);
print_r($element[0][2]);
print_r($element[24][2]);
print_r($element[44][2]);
GTT
  • 19
  • 4