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);

preferences:
53.53 ms | 1593 KiB | 5 Q