doulei2100 2018-04-12 05:06
浏览 54
已采纳

如何将sort函数包含到解析表中?

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;
  }
}
} 
  • 写回答

1条回答 默认 最新

  • dongyan6503 2018-04-12 06:04
    关注

    Updated

    You have to loop through the $results array and then loop through each players results to output the data like you want. This should work.

     $str =  "<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>
    <tr><td CLASS=tdp>SG</td><td CLASS=tdp>Jamal Murray</td><td>19</td><td>39</td><td>47</td><td>21</td><td>88</td><td>63</td><td>33</td><td>14</td><td>29</td><td>30</td><td>42</td><td>61</td><td>13</td><td>7</td><td>5</td><td>3</td><td>3</td><td>5</td><td>4</td><td>3</td><td>5</td></tr>
    <tr><td CLASS=tdp>SF</td><td CLASS=tdp>Luol Deng</td><td>31</td><td>32</td><td>43</td><td>13</td><td>73</td><td>36</td><td>31</td><td>27</td><td>48</td><td>16</td><td>45</td><td>82</td><td>13</td><td>5</td><td>3</td><td>4</td><td>1</td><td>7</td><td>6</td><td>5</td><td>6</td></tr>
    <tr><td CLASS=tdp>SF</td><td CLASS=tdp>Dante Cunningham</td><td>29</td><td>19</td><td>58</td><td>5</td><td>59</td><td>36</td><td>39</td><td>21</td><td>41</td><td>7</td><td>33</td><td>90</td><td>17</td><td>8</td><td>2</td><td>4</td><td>1</td><td>5</td><td>4</td><td>4</td><td>5</td></tr>
    <tr><td CLASS=tdp>PF</td><td CLASS=tdp>Thomas Robinson</td><td>25</td><td>55</td><td>54</td><td>32</td><td>47</td><td>0</td><td>0</td><td>83</td><td>67</td><td>15</td><td>57</td><td>55</td><td>14</td><td>4</td><td>4</td><td>9</td><td>1</td><td>9</td><td>5</td><td>9</td><td>5</td></tr>
    <tr><td CLASS=tdp>PF</td><td CLASS=tdp>Ryan Kelly</td><td>24</td><td>38</td><td>45</td><td>32</td><td>69</td><td>23</td><td>14</td><td>25</td><td>58</td><td>12</td><td>34</td><td>79</td><td>19</td><td>1</td><td>3</td><td>7</td><td>1</td><td>7</td><td>4</td><td>6</td><td>5</td></tr>
    <tr><td CLASS=tdp> C</td><td CLASS=tdp>Dewayne Dedmon</td><td>27</td><td>33</td><td>62</td><td>20</td><td>70</td><td>0</td><td>0</td><td>57</td><td>79</td><td>10</td><td>36</td><td>73</td><td>43</td><td>5</td><td>3</td><td>9</td><td>1</td><td>8</td><td>5</td><td>9</td><td>4</td></tr>
    <tr><td CLASS=tdp> C</td><td CLASS=tdp>Robert Sacre</td><td>26</td><td>38</td><td>41</td><td>33</td><td>66</td><td>0</td><td>0</td><td>56</td><td>34</td><td>12</td><td>15</td><td>80</td><td>28</td><td>1</td><td>4</td><td>7</td><td>3</td><td>5</td><td>2</td><td>7</td><td>2</td></tr>
    <tr><td CLASS=tdp>SG</td><td CLASS=tdp>Vince Carter</td><td>40</td><td>19</td><td>42</td><td>20</td><td>77</td><td>54</td><td>38</td><td>12</td><td>33</td><td>23</td><td>47</td><td>83</td><td>20</td><td>7</td><td>3</td><td>4</td><td>2</td><td>5</td><td>4</td><td>3</td><td>5</td></tr>
    <tr><td CLASS=tdp>SF</td><td CLASS=tdp>Nicolas Brussino</td><td>23</td><td>13</td><td>50</td><td>11</td><td>77</td><td>51</td><td>31</td><td>27</td><td>34</td><td>24</td><td>39</td><td>74</td><td>13</td><td>5</td><td>4</td><td>3</td><td>1</td><td>6</td><td>4</td><td>4</td><td>5</td></tr>
    <tr><td CLASS=tdp>PF</td><td CLASS=tdp>Drew Gooden</td><td>34</td><td>31</td><td>42</td><td>12</td><td>64</td><td>38</td><td>17</td><td>50</td><td>49</td><td>11</td><td>31</td><td>77</td><td>31</td><td>1</td><td>3</td><td>4</td><td>1</td><td>6</td><td>3</td><td>7</td><td>3</td></tr>
    </table>";
    
    $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];
    
    }
    
    $header = $rows[0][0]; //<----Added this.
    
    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;
    
      }
    
    }
    
     echo
    '<table>';
    
    echo $header; //<--Changed to this.
    
    foreach($results as $key){
    
      echo
      '<tr>';
    
        foreach($key as $k=>$v){
    
          echo
          '<td>' . $v . '</td>';
    
        }
    
      echo
      '</tr>';
    
    }
    
    echo
    '</table>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退