lado-casino - 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 lado-casino.php

  1. <?php
  2. $source = [
  3.     [
  4.         "casino"  => "10 BET",
  5.         "dro"     => "22:15",
  6.         "matchi"  => "Zurich - Basel",
  7.         "result1" => "1.50",
  8.         "result2" => "1.50",
  9.         "result3" => "2.50"
  10.     ],
  11.     [
  12.         "casino"  => "10 BET",
  13.         "dro"     => "22:15",
  14.         "matchi"  => "Barcelona - real",
  15.         "result1" => "3.50",
  16.         "result2" => "1.50",
  17.         "result3" => "2.50"
  18.     ],
  19.     [
  20.         "casino"  => "20 BET",
  21.         "dro"     => "22:15",
  22.         "matchi"  => "Zurich - Basel",
  23.         "result1" => "4.50",
  24.         "result2" => "1.50",
  25.         "result3" => "2.50"
  26.     ],
  27. ];
  28.  
  29. function array_group_by($array, $key, $mapCb)
  30. {
  31.     $group = [];
  32.     foreach ($array as $value) {
  33.         if (empty($group[$value[$key]])) {
  34.             $group[$value[$key]] = [];
  35.         }
  36.         array_push($group[$value[$key]], $mapCb($value));
  37.     }
  38.  
  39.     return $group;
  40. }
  41.  
  42. function print_max_results_table($array)
  43. {
  44.     echo '<table border="1">';
  45.     echo '<tr>';
  46.     echo '<th>Type</th>';
  47.     echo '<th>Casino</th>';
  48.     echo '<th>Result</th>';
  49.     echo '</tr>';
  50.  
  51.     foreach ($array as $key => $value) {
  52.  
  53.         echo '<tr>';
  54.         echo "<td>";
  55.             if($key === 0){
  56.                 echo '1';
  57.             }
  58.             if($key === 1){
  59.                 echo 'x';
  60.             }
  61.             if($key === 2){
  62.                 echo '2';
  63.             }
  64.         echo "</td>";
  65.         echo "<td>{$value['casino']}</td>";
  66.         echo "<td>{$value['result']}</td>";
  67.         echo '</tr>';
  68.     }
  69.     echo '</table>';
  70. }
  71.  
  72. function print_casinos_table($array)
  73. {
  74.     echo '<table border="1">';
  75.     echo '<tr>';
  76.     echo '<th>Casino</th>';
  77.     echo '<th>1</th>';
  78.     echo '<th>x</th>';
  79.     echo '<th>2</th>';
  80.     echo '</tr>';
  81.  
  82.     foreach ($array as $value) {
  83.         echo '<tr>';
  84.         echo "<th>{$value['casino']}</th>";
  85.         echo "<th>{$value['result1']}</th>";
  86.         echo "<th>{$value['result2']}</th>";
  87.         echo "<th>{$value['result3']}</th>";
  88.         echo '</tr>';
  89.     }
  90.     echo '</table>';
  91. }
  92.  
  93. function print_match_list($array)
  94. {
  95.     echo '<ul style="list-style: none">';
  96.     foreach ($array as $item) {
  97.         echo '<li style="display: block">';
  98.         echo "Match: <b>{$item['matchi']}</b>";
  99.         echo "<br>";
  100.         echo '<br>Max Results: ';
  101.         print_max_results_table($item['max_results']);
  102.         echo '<br>Casinos: ';
  103.         print_casinos_table($item['casinos']);
  104.         echo '<hr>';
  105.         echo '</li>';
  106.     }
  107.     echo '</ul>';
  108. }
  109.  
  110. function get_grouped($source)
  111. {
  112.     return array_group_by($source, "matchi", function ($value) {
  113.         return [
  114.             "casino"  => $value['casino'],
  115.             "result1" => (float) $value['result1'],
  116.             "result2" => (float) $value['result2'],
  117.             "result3" => (float) $value['result3']
  118.         ];
  119.     });
  120. }
  121.  
  122. function get_casino_max_results($arr)
  123. {
  124.     $r1 = [
  125.         'casino' => '',
  126.         'result' => 0
  127.     ];
  128.     $r2 = [
  129.         'casino' => '',
  130.         'result' => 0
  131.     ];
  132.     $r3 = [
  133.         'casino' => '',
  134.         'result' => 0
  135.     ];
  136.     foreach ($arr as $key => $value) {
  137.         if ($r1['result'] < $value['result1']) {
  138.             $r1['result'] = $value['result1'];
  139.             $r1['casino'] = $value['casino'];
  140.         }
  141.         if ($r2['result'] < $value['result2']) {
  142.             $r2['result'] = $value['result2'];
  143.             $r2['casino'] = $value['casino'];
  144.         }
  145.         if ($r3['result'] < $value['result3']) {
  146.             $r3['result'] = $value['result3'];
  147.             $r3['casino'] = $value['casino'];
  148.         }
  149.     }
  150.  
  151.     return [
  152.         $r1,
  153.         $r2,
  154.         $r3,
  155.     ];
  156. }
  157.  
  158. function get_mapped($array)
  159. {
  160.     $result = [];
  161.     foreach ($array as $key => $value) {
  162.         $result[] = [
  163.             'matchi'      => $key,
  164.             'max_results' => get_casino_max_results($value),
  165.             'casinos'     => $value,
  166.         ];
  167.     }
  168.  
  169.     return $result;
  170. }
  171.  
  172. $result = get_mapped(get_grouped($source));
  173.  
  174. function array_print($array)
  175. {
  176.     print( "<pre>" . print_r($array, true) . "</pre>" );
  177. }
  178.  
  179. print_match_list($result);
  180.  
  181.  
File Description
  • lado-casino
  • PHP Code
  • 25 Apr-2019
  • 4.15 Kb
You can Share it: