/
report_parser.php
112 lines (88 loc) · 4.27 KB
/
report_parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
// Open, read, save the contents, then close the file
$filename = 'report.txt';
$handle = fopen($filename, 'r');
$contents = fread($handle, filesize($filename));
fclose($handle);
// TODO: Use the file report.txt to output its contents
// Output should be ordered by sales units (high to low)
// Columns should appear as: Sales Units | Full Name | Employee Number
// Create a function that parses txt file into an array by exploding each line into an array element
$contentsArray = explode("\n", $contents);
// Delete the line dividers
$equalsLineDivider = "===================================================";
$asteriskLineDivider = "***************************************************";
$oldHeader = "Employee Number, First Name, Last Name, Sales Units";
$dividers = array($equalsLineDivider, $asteriskLineDivider, $oldHeader);
$contentsArray = array_diff($contentsArray, $dividers);
// print_r($contentsArray);
// Separate first 3 elements (to get rid of unnecessary top content) into their own array for later use
$reportHeaderArray = array_slice($contentsArray, 0, 3);
$contentsArray = array_slice($contentsArray, 3);
// print_r($reportHeaderArray);
// print_r($contentsArray);
// foreach loop through each line and explode on the commas, creating smaller arrays containing 3 elements each
// Change the numeric indices to strings by pushing onto a new empty array with predefined indices at each pass of the loop
foreach($contentsArray as $key => $eachEmployeeData){
$employeeStats = explode(', ', $eachEmployeeData);
$parentArray[$key]['Employee Number'] = $employeeStats[0]; // Renames the 0 index to 'Employee Numeber'
$parentArray[$key]['First Name'] = $employeeStats[1]; //Renames the 1 index to 'First Name'
$parentArray[$key]['Last Name'] = $employeeStats[2]; //Renames the 2 index to 'Last Name'
$parentArray[$key]['Unit Sales'] = $employeeStats[3]; //Renames the 3 index to 'Unit Sales'
}
// print_r($parentArray);
// Then sort this new array by "Sales Units"
// The following function was found on PHP.net under the 'sort' documentation
function array_sort($array, $on, $order=SORT_ASC)
{
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
$sortedBySalesArray = array_sort($parentArray, 'Unit Sales', SORT_DESC); // Sort decending order: highest to lowest
// print_r($sortedBySalesArray);
// Echo the Header information
echo $asteriskLineDivider."**************************".PHP_EOL;
echo "$reportHeaderArray[2] | $reportHeaderArray[0] | $reportHeaderArray[1]".PHP_EOL;
echo $equalsLineDivider."==========================".PHP_EOL;
echo "Sales Units | Employee Name | Employee Number".PHP_EOL;
echo "_____________________________________________________________________________".PHP_EOL;
// Create a function that foreach loops through the new global array, and:
// echo "$Array[$key]['Sales Units'] | $Array[$key]['First Name'] $Array[$key]['Last Name'] | $Array[$key]['Employee Number']";
foreach($sortedBySalesArray as $employee){
// echo "\t".$employee['Unit Sales']."\t\t".$employee['First Name']." ".$employee['Last Name']."\t\t\t".$employee['Employee Number'].PHP_EOL;
$sales = $employee['Unit Sales'];
$name = $employee['First Name']." ".$employee['Last Name'];
$employeeNum = $employee['Employee Number'];
// One formatting option:
// printf("%' 8d%' 32s%' 25u\n", $sales, $name, $employeeNum);
// Nicer formatting option:
$format = str_pad($sales, 15, " ", STR_PAD_BOTH);
$format .= str_pad($name, 35, " ", STR_PAD_BOTH);
$format .= str_pad($employeeNum, 25, " ", STR_PAD_BOTH).PHP_EOL;
echo $format;
}