3v4l.org

run code in 300+ PHP versions simultaneously
<?php $d = array ( 0 => (object) array( 'b_id' => 1, 'd' => 0, 'lo' => 10, 'm' => 4000, 'pts' => array ( 0 => (object) array( 'amount' => 3500, 'b_id' => 1, 'fid' => 2, 'side' => 2, 'user_id' => 27, ), 1 => (object) array( 'amount' => 3000, 'b_id' => 1, 'fid' => 2, 'side' => 1, 'user_id' => 27, ), 2 => (object) array( 'amount' => 2000, 'b_id' => 1, 'fid' => 2, 'side' => 2, 'user_id' => 20, ), 3 => (object) array( 'amount' => 1000, 'b_id' => 1, 'fid' => 2, 'side' => 1, 'user_id' => 20, ), ), 'ro' => 10, 'side' => 2, 'user_id' => 27, 'w' => 5000, ), 1 => (object) array( 'b_id' => 2, 'd' => 0, 'lo' => 10, 'm' => 6000, 'pts' => array ( 0 => (object) array( 'amount' => 4000, 'b_id' => 2, 'fid' => 2, 'side' => 1, 'user_id' => 27, ), 1 => (object) array( 'amount' => 2000, 'b_id' => 2, 'fid' => 2, 'side' => 2, 'user_id' => 27, ), 2 => (object) array( 'amount' => 1000, 'b_id' => 2, 'fid' => 2, 'side' => 2, 'user_id' => 20, ), 3 => (object) array( 'amount' => 2000, 'b_id' => 2, 'fid' => 2, 'side' => 1, 'user_id' => 20, ), ), 'ro' => 9, 'side' => 1, 'user_id' => 27, 'w' => 3000, ), ); function getOdds($bets, $a, $b) { return $bets * $b / $a; } function getMax($bets, $bet, $max) { $ret = 0; if($bets < $max) { $bets += $bet; $ret = $bets % $max; if($ret == $bets) { $ret = 0; } } else { $ret = $bet; } return $ret; } $break = "\n"; // <br> $break2 = "\n\n"; // <br><br> $collections = []; foreach($d as $row) { $odds = getOdds($row->m, $row->lo, $row->ro); $collection = []; foreach(array_column($row->pts, 'amount') as $amount) { $ret = getMax($row->m, $amount, $odds); // I am assuming that $row-m is supposed to be $bets $amount = round(abs($amount - $ret), 2); $collection[] = implode($break, ["Odds: {$odds}", "Ret: {$ret}", "Amount: {$amount}"]); } $collections[] = implode($break, [ "======[START (b_id: {$row->b_id}]======", implode($break2, $collection), "=======[END (b_id: {$row->b_id}]=======", ]); } echo implode($break2, $collections);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
======[START (b_id: 1]====== Odds: 4000 Ret: 3500 Amount: 0 Odds: 4000 Ret: 3000 Amount: 0 Odds: 4000 Ret: 2000 Amount: 0 Odds: 4000 Ret: 1000 Amount: 0 =======[END (b_id: 1]======= ======[START (b_id: 2]====== Odds: 5400 Ret: 4000 Amount: 0 Odds: 5400 Ret: 2000 Amount: 0 Odds: 5400 Ret: 1000 Amount: 0 Odds: 5400 Ret: 2000 Amount: 0 =======[END (b_id: 2]=======

preferences:
122.87 ms | 1591 KiB | 4 Q