$data = Array(
            Array(,,32000,),
            Array(,,47000,),
            Array(,,53500,),
            Array(,,18000,),
            Array(,,27800,),
            Array(,,41500,),
            Array(,,49500,),
            Array(,,21900,),
            Array(,,15900,),
            Array(,,19250,),
            Array(,,27000,),
            Array(,,57000,),
            Array(,,29900,)
            );
function top_sal($num){
    global $data;
    $depts = Array();
    foreach($data as $key => $arr){
        if(!isset($depts[$arr[3]])) $depts[$arr[3]] = Array();
        $depts[$arr[3]][] = $key;
    }   
    function topsalsort($a,$b){
        global $data;
        if ($data[$a][2] == $data[$b][2]) {
            return 0;
        }
        return ($data[$a][2] < $data[$b][2])? 1 : -1;
    }
    foreach ($depts as $key => $val){
        usort($depts[$key],);
    }
    ksort($depts);
    echo '<pre>';
    foreach ($depts as $key => $val){
        echo $key . '<br>';
        echo 'Name          ID      Salary<br>';
        $count = 0;
        foreach($val as $value){
            echo $data[$value][0] . '   ' . $data[$value][1] . '    ' . $data[$value][2] . '<br>';
            $count++;
            if($count>=$num) break;
        }
        echo '<br>';
    }
    echo '</pre>';
}
top_sal(3);