Created
November 19, 2017 10:25
-
-
Save tihoho/8cd42d1e237bfdc2b35f12582c286190 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Входящие данные | |
$article = 'hp'; // артикул товара | |
$sum = 3563; //Сумма (для теста тут принтер hp + 4 катриджа: hpb*3 + hpr*1) | |
// принтеры | |
$printersPrice = [ | |
'canon' => 1000, | |
'hp' => 2000, | |
'xerox' => 3000, | |
]; | |
// катриджи | |
$accessories = [ | |
'canon' => [ | |
'cnb' => 390, | |
'cnbxl' => 490, | |
], | |
'hp' => [ | |
'hpr' => 390, | |
'hpb' => 391, | |
'hpg' => 392, | |
'hpk' => 393, | |
], | |
]; | |
$accessoriesSum = $sum - $printersPrice[$article]; // вычитаем стоймость принтера из общей суммы | |
$basket = []; | |
function makeBasket($sum, $prices, $result) { | |
if ($sum % $prices[0] == 0) { | |
return array_merge($result, [$sum / $prices[0]]); | |
} | |
if (count($prices) == 1) { | |
return false; | |
} | |
for ($i = 0; $i <= $sum / $prices[0]; $i++) { | |
$res = makeBasket($sum - $prices[0] * $i, array_slice($prices, 1), array_merge($result, [$i])); | |
if ($res != false) { | |
return $res; | |
} | |
} | |
return false; | |
} | |
$prices = array_values($accessories[$article]); | |
$res = makeBasket($accessoriesSum, $prices, []); | |
$result = []; | |
$i = 0; | |
foreach ($accessories[$article] as $key => $val) { | |
if (($i < count($res)) && ($res[$i] != 0)) { | |
$result[$key] = $res[$i]; | |
} | |
$i++; | |
} | |
print_r($result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment