I want to sort a parsed html-table (via simple_html_dom) by two of its columns and for some part by a specific order before echoing it. Its the first column with <th>pos</th>
and the <td's>
containing "PG" "SG" "SF" "PF" and "C" for basketball positions. At a second level it should get sort by the 4th column containing the shot volume (th = 2ga
, td's are numeric)
This should be the order:
first priority: "pos" = 0 "PG" = 1 "SG" = 2 "SF" = 3 "PF" = 4 "C" = 5.
second priority: "2ga" = 0, numbers descending, biggest value at first
I checked the various sort function php has to offer, but I'm honestly a little bit overwhelmed by the depth of it since I'm kinda new to php. Here an example of the source code:
<table><hr>
<tr><th>po</th><th>player</th><th>age</th><th>2ga</th><th>2g%</th><th>fta</th><th>ft%</th><th>3ga</th><th>3g%</th><th>orb</th><th>drb</th><th>ast</th><th>stl</th><th>to</th><th>blk</th><th>o-o</th><th>d-o</th><th>p-o</th><th>t-o</th><th>o-d</th><th>d-d</th><th>p-d</th><th>t-d</th></tr>
<tr><td CLASS=tdp>PG</td><td CLASS=tdp>James Harden</td><td>27</td><td>48</td><td>53</td><td>95</td><td>85</td><td>85</td><td>35</td><td>20</td><td>59</td><td>99</td><td>57</td><td>1</td><td>12</td><td>4</td><td>9</td><td>7</td><td>9</td><td>8</td><td>6</td><td>5</td><td>7</td></tr>
<tr><td CLASS=tdp>PG</td><td CLASS=tdp>Dennis Schroder</td><td>23</td><td>68</td><td>49</td><td>32</td><td>85</td><td>39</td><td>34</td><td>10</td><td>25</td><td>65</td><td>42</td><td>35</td><td>6</td><td>8</td><td>8</td><td>2</td><td>6</td><td>5</td><td>4</td><td>2</td><td>6</td></tr>
<tr><td CLASS=tdp>SG</td><td CLASS=tdp>Evan Fournier</td><td>24</td><td>46</td><td>49</td><td>39</td><td>81</td><td>53</td><td>36</td><td>12</td><td>23</td><td>29</td><td>41</td><td>61</td><td>1</td><td>7</td><td>5</td><td>4</td><td>4</td><td>5</td><td>4</td><td>2</td><td>6</td></tr>
... more rows ...
</table>
I'm stuck currently at this point. I especially don't know where I would have to put the sort function.
$table = $html->find('table', 1);
$rowData = array();
// Loop
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$roster = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$roster[] = $cell->innertext;
}
foreach($row->find('th') as $cell) {
// push the cell's text to the array
$roster[] = $cell->innertext;
}
$rowData[] = $roster;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
One possible sort function working the source html-code:
$rowData = $str;
$str = preg_replace('/\s{1,}/', ' ', $str);
preg_match_all('/<tr>(.*?)<\/tr>/', $str, $rows);
foreach($rows[1] as $row){
preg_match_all('/<td(.*?)<\/td>/', $row, $data);
$results[] = $data[0];
}
unset($results[0]);
array_walk_recursive($results, function(&$a, $b){
$a = strip_tags($a);
$a = trim($a);
});
$results = array_values($results);
for($i = 0; $i < count($results); $i++){
switch($results[$i][0]){
case 'PG':
$results[$i][0] = 1;
break;
case 'SG':
$results[$i][0] = 2;
break;
case 'SF':
$results[$i][0] = 3;
break;
case 'PF':
$results[$i][0] = 4;
break;
case 'C':
$results[$i][0] = 5;
break;
}
}
usort($results, function($a, $b){
if([$a[0], $a[4]] < [$b[0], $b[4]]){
return 1;
}elseif([$a[0], $a[4]] > [$b[0], $b[4]]){
return -1;
}else{
return 0;
}
});
for($i = 0; $i < count($results); $i++){
switch($results[$i][0]){
case 1:
$results[$i][0] = 'PG';
break;
case 2:
$results[$i][0] = 'SG';
break;
case 3:
$results[$i][0] = 'SF';
break;
case 4:
$results[$i][0] = 'PF';
break;
case 5:
$results[$i][0] = 'C';
break;
}
}
}