- var_dump: documentation ( source)
<?php
// create array of objects
$arr = [new stdClass, new stdClass, new StdClass];
// assign properties
for($i=0, $max=count($arr); $i < $max; $i++) {
$arr[$i]->age = 0;
$arr[$i]->creature = "squirrel";
}
// update age values
$arr[0]->age = 10;
$arr[1]->age = 25;
$arr[2]->age = 30;
// recursively sum the ages of the array of objects ...
function getTotalYears( $arr, $i, $carry ) {
if ($i >= ( count( $arr ) - 1 ) ){
return $carry;
}
else
{
$carry += $arr[$i++]->age;
getTotalYears( $arr, $i, $carry );
}
}
// .. and display result
$total = getTotalYears( $arr, 0, 0);
var_dump($total);
echo "\nTotal years of nut foraging experience: $total";