function test($array, $cost)
{
$group = [];
foreach ($array as $item){
$group[$item] = !empty($group[$item]) ? $group[$item] + 1 : 1;
}
$tmp_cost = $cost;
$certs = [];
foreach ($group as $amount => $count){
while ($count > 0) {
if(($tmp_cost - $amount) == 0) {
$tmp_cost = $tmp_cost - $amount;
$certs[] = $amount;
break;
} elseif($tmp_cost - $amount > 0) {
$tmp_cost = $tmp_cost - $amount;
$certs[] = $amount;
}
$count--;
}
}
return array_sum($certs) == $cost ? $certs : false;
}
$cost = 3550;
$array = [
'0' => 2000,
'1' => 2000,
'2' => 2000,
'3' => 2000,
'4' => 2000,
'5' => 2000,
'6' => 2000,
'7' => 1000,
'8' => 200,
'9' => 200,
'10' => 200,
'11' => 200,
'12' => 200,
'13' => 200,
'14' => 200,
'15' => 200,
'16' => 100,
'17' => 100,
'18' => 100,
'19' => 100,
'20' => 100,
'21' => 100,
'22' => 100,
'23' => 100,
'24' => 100,
'25' => 100,
'26' => 100,
'27' => 100,
'28' => 100,
'29' => 100,
'30' => 100,
'31' => 100,
'32' => 100,
'33' => 100,
'34' => 50,
'35' => 50,
'36' => 50,
'37' => 50,
'38' => 50,
'39' => 50,
'40' => 50,
'41' => 50,
'42' => 50,
'43' => 50,
'44' => 50,
'45' => 50,
'46' => 50,
'47' => 50,
'48' => 50,
'49' => 50,
'50' => 50,
'51' => 50
];
print_r(test($array, $cost));
Array
(
[0] => 2000
[1] => 1000
[2] => 200
[3] => 200
[4] => 100
[5] => 50
)