I know the PHP end() function exists, but i don't just want the last value of the object, i want the last 500 values.
I have an object with possibly thousands of data and i want to show a graph on my frontend with this data. But only the last 500 values as more than that could slow down the response time too much.
I was thinking of maybe reversing the values of the data object, take the first 500 values and then reverse it back again. This just sounds like too much wasted processing.
I've also tried to first convert the object to an array and then using the array_slice($array, -500);
and the converting it back to a PHP object and use it in a foreach
. When i do this, i get the error:
Invalid argument supplied for foreach()
This is the code:
$dataArray = array_slice((array) $data, -5);
$data = json_encode($dataArray);
Do you have any ideas what i'm doing wrong here or how to do this smarter?