I am trying to write a solution which sorts some output from one of our old legacy applications but I'm having some problems. I wrote a non OO implementation that works, albeit with some warnings but that's fine because at least I'm getting the result I want. Now that I've tried to move it over to an OO implementation nothing I want is happening, if anything but errors at all.
This is a sample file I wrote for testing purposes:
COL1 COL2 COL3 COL4 COL5
val1a val2a val3a val4a val5a
val1b val2b val3b val4b val5b
val1c val2c val3c val4c val5c
val1d val2d val3d val4d val5d
val1e val2e val3e val4e val5e
val1f val2f val3f val4f val5f
val1g val2g val3g val4g val5g
And here is the non OO code that works:
<?php
$file = file_get_contents("tableinput.txt");
$table = array();
$temp_table = array();
$file = explode ("\r\n", $file); //break the input into individual lines
//we assume file was written on Windows machine (can easily change \r\n to simply \r for Linux)
foreach ($file as $line) {
$line = explode ("\t", $line);
array_push($temp_table, $line);
}
for($i = 0; $i < sizeof($temp_table); $i++){
$col_array = array();
for($j = 1; $j < sizeof($temp_table); $j++){
array_push($col_array, $temp_table[$j][$i]);
}
if($temp_table[0][$i] != "")
$table[$temp_table[0][$i]] = $col_array;
}
//sort array based on data in COL1 in descending format.
//Since input data is already sorted in ascending order,
//this is the easiest way to show that this is effective
array_multisort($table['COL1'], SORT_DESC, SORT_STRING, $table['COL2'], $table['COL3'], $table['COL4'], $table['COL5']);
echo "<pre>"; var_export($table); echo "</pre>"; //dump the sorted array to the screen (proof of concept)
?>
And here, alas, is the code that for reasons that are beyond me, does not.
<?php
class SortTable {
public $table = array();
public function __construct ($filepath) {
$file = file_get_contents($filepath);
$temp_table = array();
$file = explode ("\r\n", $file); //break the input into individual lines
//we assume file was written on Windows machine (can easily change \r\n to simply \r for Linux)
foreach ($file as $line) {
$line = explode ("\t", $line);
array_push($temp_table, $line);
}
for($i = 0; $i < count($temp_table); $i++){
$col_array = array();
for($j = 1; $j < count($temp_table); $j++){
array_push($col_array, $temp_table[$j][$i]);
}
if($temp_table[0][$i] != "")
$this->table[$temp_table[0][$i]] = $col_array;
}
}
public function GetSortedTable ($SortCol, $params) {
/*$ParamSet = Array();
array_push ($ParamSet, &$this->table[$SortCol]);
array_push ($ParamSet, explode(',', $params));
foreach ($this->table as $column) {
if ($column != $this->table[$SortCol]) {
array_push ($ParamSet, &$column);
}
} */
//call_user_func_array('array_multisort', $ParamSet);
array_multisort ($this->table['COL1'], SORT_DESC, SORT_STRING, $this->table['COL2'], $this->table['COL3'], $this->table['COL4'], $this->table['COL5']);
return $this->table;
}
}
$sort = New SortTable("http://root.pixomania.net/table.txt");
$table = $sort->GetSortedTable("COL3", "SORT_DESC, SORT_STRING");
echo "<pre>"; var_export($table); echo "</pre>"; //dump the sorted array to the screen (proof of concept)
?>
Here is the output from the non OO which works. This is exactly what I'm trying to get from the OO version (if I could manage to get rid of those warnings, that would be great, but beggars can sooooo not be choosers.)
Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 5 in E:\wamp\www\Mike Broudy\tableproc.php on line 23
Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 6 in E:\wamp\www\Mike Broudy\tableproc.php on line 23
Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 21
Notice: Undefined offset: 7 in E:\wamp\www\Mike Broudy\tableproc.php on line 23
array (
'COL1' =>
array (
0 => 'val1g',
1 => 'val1f',
2 => 'val1e',
3 => 'val1d',
4 => 'val1c',
5 => 'val1b',
6 => 'val1a',
),
'COL2' =>
array (
0 => 'val2g',
1 => 'val2f',
2 => 'val2e',
3 => 'val2d',
4 => 'val2c',
5 => 'val2b',
6 => 'val2a',
),
'COL3' =>
array (
0 => 'val3g',
1 => 'val3f',
2 => 'val3e',
3 => 'val3d',
4 => 'val3c',
5 => 'val3b',
6 => 'val3a',
),
'COL4' =>
array (
0 => 'val4g',
1 => 'val4f',
2 => 'val4e',
3 => 'val4d',
4 => 'val4c',
5 => 'val4b',
6 => 'val4a',
),
'COL5' =>
array (
0 => 'val5g',
1 => 'val5f',
2 => 'val5e',
3 => 'val5d',
4 => 'val5c',
5 => 'val5b',
6 => 'val5a',
),
)
You guys have always been great to me, I learn so much more every time I post here, so here's hoping you guys can point out all my retarded mistakes so I never have to make them again.
--David