Advertisement
DvDty

Untitled

Jul 1st, 2020
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1. <?php
  2.  
  3. $data = file_get_contents('https://gist.githubusercontent'
  4.     . '.com/jorostoyanov/17b41811d85e9cb81351eecffd81efec/raw/'
  5.     . '473a25c450161a22f0adcba841c230ba9ef83b40/employees.json');
  6.  
  7. $data = json_decode($data, true);
  8.  
  9. function get_employees_names($data) {
  10.     return array_column($data, 'name');
  11. }
  12.  
  13. function get_salary_range($data) {
  14.     $result = [];
  15.  
  16.     $result['min'] = $result['max'] = $data[0]['salary'];
  17.  
  18.     foreach ($data as $employee) {
  19.         if ($employee['salary'] < $result['min']) {
  20.             $result['min'] = $employee['salary'];
  21.         }
  22.  
  23.         if ($employee['salary'] > $result['max']) {
  24.             $result['max'] = $employee['salary'];
  25.         }
  26.     }
  27.  
  28.     return $result;
  29. }
  30.  
  31. function get_positions($data) {
  32.     $positions = [];
  33.  
  34.     foreach ($data as $employee) {
  35.         if (!array_key_exists($employee['position'], $positions)) {
  36.             $positions[$employee['position']] = 1;
  37.             continue;
  38.         }
  39.  
  40.         $positions[$employee['position']]++;
  41.     }
  42.  
  43.     return $positions;
  44. }
  45.  
  46. function get_teams($data) {
  47.     $teams = [];
  48.  
  49.     foreach ($data as $employee) {
  50.         if (!array_key_exists($employee['team'], $teams)) {
  51.             $teams[$employee['team']] = [];
  52.         }
  53.  
  54.         $teams[$employee['team']][] = [
  55.             'name' => $employee['name'],
  56.             'position' => $employee['position'],
  57.             'salary' => $employee['salary'],
  58.         ];
  59.     }
  60.  
  61.     return $teams;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement