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.
Result of php executing
Full code of 21.php
- <?php
- $order_products = [
- '102' => [
- 'price' => 31,
- 'count' => 5
- ],
- '103' => [
- 'price' => 67,
- 'count' => 3
- ]
- ];
- $order_products = [
- '101' => [
- 'price' => 1179,
- 'count' => 2
- ],
- '102' => [
- 'price' => 533,
- 'count' => 2
- ],
- '103' => [
- 'price' => 690,
- 'count' => 3
- ]
- ];
- $discount = 600;
- $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'];
- }
- //var_dump($order_products);
- $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;
- }
- }
- var_dump($order_products);
- print $result . "\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";
File Description
- 21
- PHP Code
- 07 Jan-2021
- 2.33 Kb
You can Share it:
Latest PHP Pastes