- array_sum: documentation ( source)
- print_r: documentation ( source)
- floor: documentation ( source)
- asort: documentation ( source)
<?php
$total_capacity = 100;
$resources = array('gold'=>125,'stone'=>30, 'food' => 0);
$loot = array();
asort($resources);
foreach($resources as $type => $resource)
{
$max = floor(($total_capacity-array_sum($loot))/(3-count($loot)));
// var_dump(count($loot));
if($max > $resource) {
$loot[$type] = $resource;
}
else {
$loot[$type] = $max;
}
}
print_r($loot);
?>