123 - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of 123.php
- <?php
- $order_products = [
- '102' => [
- 'price' => 31,
- 'count' => 5
- ],
- '103' => [
- 'price' => 67,
- 'count' => 3
- ]
- ];
- $order_products = [
- '101' => [
- 'price' => 1179.21,
- 'count' => 2
- ],
- '102' => [
- 'price' => 533.34,
- 'count' => 2
- ],
- '103' => [
- 'price' => 690.43,
- 'count' => 3
- ]
- ];
- $discount = 5000;
- $products = [];
- $summ = 0;
- foreach ($order_products as $order_product) {
- $summ = $summ + $order_product['price'] * $order_product['count'];
- }
- print 'сумма стоимости по всем товарам - ' . $summ . "\n";
- $ratio = $discount / $summ;
- //print $ratio . "\n";
- $ratio = floor($ratio * 1000) / 1000;
- print 'коефициент скидки - ' . $ratio . "\n";
- foreach ($order_products as $index => $order_product) {
- $discount_summ = $order_product['price'] * $ratio;
- $order_products[$index]['discount_per_product_summ_original'] = $discount_summ;
- $order_products[$index]['discount_per_product_summ'] = floor($discount_summ * 1) / 1;
- $order_products[$index]['discount_summ'] = $order_products[$index]['discount_per_product_summ'] * $order_product['count'];
- }
- $discount_summ = 0;
- foreach ($order_products as $order_product) {
- $discount_summ = $discount_summ + $order_product['discount_summ'];
- }
- print 'предварительная сумма скидки по всем товарам - ' . $discount_summ . "\n";
- $diff = round($discount - $discount_summ, 2);
- //print $diff . "\n";
- $result = false;
- foreach ($order_products as $index => $order_product) {
- //print ($diff * 1) % $order_product['count'] . "\n";
- if ((($diff * 1) % $order_product['count']) == 0) {
- $part = $diff / $order_product['count'];
- $order_products[$index]['part'] = $part;
- $order_products[$index]['discount_per_product_summ_finish'] = round($order_products[$index]['discount_per_product_summ'] + $part, 2);
- $result = true;
- break;
- }
- }
- print 'распределение удалось - ' . ($result ? 'true' : 'false') . "\n";
- $summ_finish = 0;
- foreach ($order_products as $order_product) {
- $summ_finish = $summ_finish + ($order_product['discount_per_product_summ_finish'] ?? $order_product['discount_per_product_summ']) * $order_product['count'];
- }
- print 'сумма скидки по всем товарам - ' . $summ_finish . "\n";
- $finish = [];
- foreach ($order_products as $product_id => $order_product) {
- $finish[$product_id] = [
- 'price' => $order_product['price'],
- 'count' => $order_product['count'],
- 'discount' => $order_product['discount_per_product_summ_finish'] ?? $order_product['discount_per_product_summ'],
- 'price_with_discount' => $order_product['price'] - ($order_product['discount_per_product_summ_finish'] ?? $order_product['discount_per_product_summ'])
- ];
- }
- var_dump($finish);