21 - 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 21.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,
  17.         'count' => 2
  18.     ],
  19.     '102' => [
  20.         'price' => 533,
  21.         'count' => 2
  22.     ],
  23.     '103' => [
  24.         'price' => 690,
  25.         'count' => 3
  26.     ]
  27. ];
  28.  
  29. $discount = 600;
  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. //var_dump($order_products);
  64.  
  65. $discount_summ = 0;
  66.  
  67. foreach ($order_products as $order_product) {
  68.     
  69.     $discount_summ = $discount_summ + $order_product['discount_summ'];
  70.     
  71. }
  72.  
  73. print $discount_summ . "\n";
  74.  
  75. $diff = round($discount - $discount_summ, 2);
  76.  
  77. print $diff . "\n";
  78.  
  79. $result = false;
  80.  
  81. foreach ($order_products as $index => $order_product) {
  82.     
  83.     print ($diff * 1) % $order_product['count'] . "\n";
  84.     
  85.     if ((($diff * 1) % $order_product['count']) == 0) {
  86.         
  87.         $part = $diff / $order_product['count'];
  88.         
  89.         $order_products[$index]['part'] = $part;
  90.         
  91.         $order_products[$index]['discount_per_product_summ_finish'] = round($order_products[$index]['discount_per_product_summ'] + $part, 2);
  92.         
  93.         $result = true;
  94.         
  95.         break;
  96.         
  97.     }
  98.     
  99. }
  100.  
  101. var_dump($order_products);
  102.  
  103. print $result . "\n";
  104.  
  105. $summ_finish = 0;
  106.  
  107. foreach ($order_products as $order_product) {
  108.     
  109.     $summ_finish = $summ_finish + ($order_product['discount_per_product_summ_finish'] ?? $order_product['discount_per_product_summ']) * $order_product['count'];
  110.     
  111. }
  112.  
  113. print $summ_finish . "\n";
File Description
  • 21
  • PHP Code
  • 07 Jan-2021
  • 2.33 Kb
You can Share it: