I have an array stored into a variable called $data
["data"]=>
array(413) {
[0]=>
object(stdClass)#254 (7) {
["Energy"]=>
string(7) "44555"
["Closing"]=>
string(10) "102"
}
[1]=>
object(stdClass)#260 (7) {
["Energy"]=>
string(7) "2522"
["Closing"]=>
string(10) "854"
}
And I obtain an arithmetic mean for all the Closing values like this:
// initialize sum and total
$sum = 0;
$total = 0;
foreach ($data->data as $obj) {
if (isset($obj->Closing)) { // verify that the current object has a Closing
$sum += $obj->Closing; // add it to the sum
$total++; // increment the count
}
}
echo $sum / $total; // display the average
The problem is that now I will only need the first 30 key from the array, but I don't know how to do that, can anyone help me please? I have tried with a for loop but it doesn't do the trick. Any ideas? Thank you all in advance for your time and help.