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.



Your result can be seen below.

Result of php executing





Full code of 123.php

  1. <?php
  2.  
  3. $order_products = [
  4.     '102' => [
  5.         'price' => 31,
  6.         'count' => 5
  7.     ],
  8.     '103' => [
  9.         'price' => 67,
  10.         'count' => 3
  11.     ]
  12. ];
  13.  
  14. $order_products = [
  15.     '101' => [
  16.         'price' => 1179.21,
  17.         'count' => 2
  18.     ],
  19.     '102' => [
  20.         'price' => 533.34,
  21.         'count' => 2
  22.     ],
  23.     '103' => [
  24.         'price' => 690.43,
  25.         'count' => 3
  26.     ]
  27. ];
  28.  
  29. $discount = 5000;
  30.  
  31. $products = [];
  32.  
  33. $summ = 0;
  34.  
  35. foreach ($order_products as $order_product) {
  36.     
  37.     $summ = $summ + $order_product['price'] * $order_product['count'];
  38.     
  39. }
  40.  
  41. print 'сумма стоимости по всем товарам - ' . $summ . "\n";
  42.  
  43. $ratio = $discount / $summ;
  44.  
  45. //print $ratio . "\n";
  46.  
  47. $ratio = floor($ratio * 1000) / 1000;
  48.  
  49. print 'коефициент скидки - ' . $ratio . "\n";
  50.  
  51. foreach ($order_products as $index => $order_product) {
  52.     
  53.     $discount_summ = $order_product['price'] * $ratio;
  54.     
  55.     $order_products[$index]['discount_per_product_summ_original'] = $discount_summ;
  56.     
  57.     $order_products[$index]['discount_per_product_summ'] = floor($discount_summ * 1) / 1;
  58.     
  59.     $order_products[$index]['discount_summ'] = $order_products[$index]['discount_per_product_summ'] * $order_product['count'];
  60.     
  61. }
  62.  
  63. $discount_summ = 0;
  64.  
  65. foreach ($order_products as $order_product) {
  66.     
  67.     $discount_summ = $discount_summ + $order_product['discount_summ'];
  68.     
  69. }
  70.  
  71. print 'предварительная сумма скидки по всем товарам - ' . $discount_summ . "\n";
  72.  
  73. $diff = round($discount - $discount_summ, 2);
  74.  
  75. //print $diff . "\n";
  76.  
  77. $result = false;
  78.  
  79. foreach ($order_products as $index => $order_product) {
  80.     
  81.     //print ($diff * 1) % $order_product['count'] . "\n";
  82.     
  83.     if ((($diff * 1) % $order_product['count']) == 0) {
  84.         
  85.         $part = $diff / $order_product['count'];
  86.         
  87.         $order_products[$index]['part'] = $part;
  88.         
  89.         $order_products[$index]['discount_per_product_summ_finish'] = round($order_products[$index]['discount_per_product_summ'] + $part, 2);
  90.         
  91.         $result = true;
  92.         
  93.         break;
  94.         
  95.     }
  96.     
  97. }
  98.  
  99. print 'распределение удалось - ' . ($result 'true' : 'false') . "\n";
  100.  
  101. $summ_finish = 0;
  102.  
  103. foreach ($order_products as $order_product) {
  104.     
  105.     $summ_finish = $summ_finish + ($order_product['discount_per_product_summ_finish'] ?? $order_product['discount_per_product_summ']) * $order_product['count'];
  106.     
  107. }
  108.  
  109. print 'сумма скидки по всем товарам - ' . $summ_finish . "\n";
  110.  
  111. $finish = [];
  112.  
  113. foreach ($order_products as $product_id => $order_product) {
  114.     
  115.     $finish[$product_id] = [
  116.         'price' => $order_product['price'],
  117.         'count' => $order_product['count'],
  118.         'discount' => $order_product['discount_per_product_summ_finish'] ?? $order_product['discount_per_product_summ'],
  119.         'price_with_discount' => $order_product['price'] - ($order_product['discount_per_product_summ_finish'] ?? $order_product['discount_per_product_summ'])
  120.         
  121.     ];
  122.     
  123. }
  124.  
  125. var_dump($finish);
File Description
  • 123
  • PHP Code
  • 07 Jan-2021
  • 3.01 Kb
You can Share it: