fork download
  1. <?php
  2.  
  3. //https://i...content-available-to-author-only...e.com/qQJDfQ
  4.  
  5. $tariffs = ['tariff0' => 0, 'tariff1' => 1,'tariff2' => 3,'tariff3' => 5];
  6.  
  7. $calls = [ [ "A-number" => "79222222222",
  8. "B-number" => "79111111111",
  9. "duration" => 127,
  10. "tariff" => "tariff1"
  11. ],
  12. [ "A-number" => "79111111111",
  13. "B-number" => "79222222222",
  14. "duration" => 53,
  15. "tariff" => "tariff2"
  16. ],
  17. [ "A-number" => "79222222222",
  18. "B-number" => "79333333333",
  19. "duration" => 671,
  20. "tariff" => "tariff0"
  21. ],
  22. [ "A-number" => "79333333333",
  23. "B-number" => "79222222222",
  24. "duration" => 632,
  25. "tariff" => "tariff3"
  26. ],
  27. [ "A-number" => "79111111111",
  28. "B-number" => "79333333333",
  29. "duration" => 331,
  30. "tariff" => "tariff1"
  31. ]
  32. ];
  33.  
  34. var_dump($calls);
  35. function showCallsInfo($phoneNumber, $calls, $tariffs) {
  36. $totalCost = 0;
  37. $result = [];
  38.  
  39. foreach ($calls as $call) {
  40. if ($call['A-number'] === $phoneNumber) {
  41. $direction = 'исходящий';
  42. $cost = $call['duration'] * $tariffs[$call['tariff']];
  43. $totalCost += $cost;
  44. } elseif ($call['B-number'] === $phoneNumber) {
  45. $direction = 'входящий';
  46. $cost = 0;
  47. }
  48.  
  49. $result[] = [
  50. 'number' => $call['A-number'] === $phoneNumber ? $call['B-number'] : $call['A-number'],
  51. 'direction' => $direction,
  52. 'duration' => $call['duration'],
  53. 'tariff' => $direction === 'исходящий' ? $call['tariff'] : '',
  54. 'cost' => $cost
  55. ];
  56. }
  57.  
  58. echo "Номер: $phoneNumber\n";
  59.  
  60. foreach ($result as $call) {
  61. echo sprintf(
  62. "%s %s %d сек. %s\n",
  63. $call['number'],
  64. $call['direction'],
  65. $call['duration'],
  66. $call['tariff']
  67. );
  68. }
  69.  
  70. echo "К оплате: " . number_format($totalCost, 2) . " руб.\n";
  71. }
  72.  
  73.  
  74. showCallsInfo('79222222222', $calls, $tariffs);
  75.  
Success #stdin #stdout 0.04s 25808KB
stdin
Standard input is empty
stdout
array(5) {
  [0]=>
  array(4) {
    ["A-number"]=>
    string(11) "79222222222"
    ["B-number"]=>
    string(11) "79111111111"
    ["duration"]=>
    int(127)
    ["tariff"]=>
    string(7) "tariff1"
  }
  [1]=>
  array(4) {
    ["A-number"]=>
    string(11) "79111111111"
    ["B-number"]=>
    string(11) "79222222222"
    ["duration"]=>
    int(53)
    ["tariff"]=>
    string(7) "tariff2"
  }
  [2]=>
  array(4) {
    ["A-number"]=>
    string(11) "79222222222"
    ["B-number"]=>
    string(11) "79333333333"
    ["duration"]=>
    int(671)
    ["tariff"]=>
    string(7) "tariff0"
  }
  [3]=>
  array(4) {
    ["A-number"]=>
    string(11) "79333333333"
    ["B-number"]=>
    string(11) "79222222222"
    ["duration"]=>
    int(632)
    ["tariff"]=>
    string(7) "tariff3"
  }
  [4]=>
  array(4) {
    ["A-number"]=>
    string(11) "79111111111"
    ["B-number"]=>
    string(11) "79333333333"
    ["duration"]=>
    int(331)
    ["tariff"]=>
    string(7) "tariff1"
  }
}
Номер: 79222222222
79111111111 исходящий 127 сек. tariff1
79111111111 входящий 53 сек. 
79333333333 исходящий 671 сек. tariff0
79333333333 входящий 632 сек. 
79111111111 входящий 331 сек. 
К оплате: 127.00 руб.