数组未在foreach中修剪

时间:2015-02-03 20:24:38

标签: php arrays foreach trim

我正在使用PHP中的HTML表,试图从那里获取信息,但不知何故我的数组值没有被修剪,我无法得到正确的读数。我已经尝试了修剪功能(如示例所示)以及foreach中的内联修剪。什么都没有。

我做错了什么? 任何帮助表示赞赏!

PHP:

    <?php
ini_set ("display_errors", "1");

error_reporting(E_ALL);

    require_once("DOMe.phps");

    $dom = new DOMe("div");
    $dom->importHTML(file_get_contents("rep.html"));

#   echo $dom->generate();

    $rows = $dom->getElementsByTagName("tr");

    $data = array();
    foreach ($rows as $row) {
        $cells = $row->getElementsByTagName("td");
        $cellData = array();
        foreach ($cells as $cell) {
            $cellData[] = $cell->generate();
        }
        $data[] = $cellData;
    }
    # Trim the multidimensional array
    multiDim($data);

    print_r(otsivotit($data, "1st"));
    echo "<br />";

# Searching for a key by the value from the multidimensional array
function otsivotit($massiiv, $voti)
{
    $tulemus = array();
    foreach ($massiiv as $key => $alammassiiv)
    {
        # If value is found, save it to results array $tulemus[]
        if ($alammassiiv == $voti)
        {
            $tulemus[] = $key;
        }
        else
        {
            # If the value turns out to be another array, go deeper
            if (is_array($alammassiiv))
            {
                $tulemus = array_merge($tulemus, otsivotit($alammassiiv, $voti));
            }
        }
    }
    return $tulemus;
}

function multiDim(&$array)
{
        foreach($array as &$value)
        is_array($value) ? multiDim($value):$value=trim($value);
        unset($value);
}

?>

HTML:

<!DOCTYPE html>
<html>
<head>


    <title></title>
</head>

<body>
    <br>

    <table>
        <tr>
            <td width="200"></td>

            <td width="50;">1st</td>

            <td width="50;">2nd</td>

            <td width="50;">3rd</td>

            <td width="50;">4th</td>

            <td width="75">Final</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td width="50">Vastased</td>

            <td>27</td>

            <td>23</td>

            <td>20</td>

            <td>18</td>

            <td width="75">88</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td width="50">TĆ/Rock Fanclub</td>

            <td>13</td>

            <td>6</td>

            <td>7</td>

            <td>5</td>

            <td width="75">31</td>
        </tr>
    </table>

    <table width="100%">
        <tr>
            <td>Player</td>

            <td>GAMES</td>

            <td>MIN</td>

            <td>FG</td>

            <td>FG%</td>

            <td>2P</td>

            <td>2P%</td>

            <td>3P</td>

            <td>3P%</td>

            <td>FT</td>

            <td>FT%</td>

            <td>+/-</td>

            <td>REB</td>

            <td>OREB</td>

            <td>DREB</td>

            <td>AST</td>

            <td>STL</td>

            <td>DEFL</td>

            <td>BLK</td>

            <td>TO</td>

            <td>EFF</td>

            <td>PF</td>

            <td>PTS</td>

            <td>AST/TO</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td></td>
        </tr>

        <tr>
            <td>#null null</td>

            <td>1</td>

            <td>0:00</td>

            <td>37/71</td>

            <td>52,1%</td>

            <td>32/52</td>

            <td>61,5%</td>

            <td>5/19</td>

            <td>26,3%</td>

            <td>9/26</td>

            <td>34,6%</td>

            <td>0</td>

            <td>40</td>

            <td>18</td>

            <td>22</td>

            <td>4</td>

            <td>15</td>

            <td>4</td>

            <td>1</td>

            <td>7</td>

            <td>90</td>

            <td>16</td>

            <td>88</td>

            <td>0.5714285714285714</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td></td>
        </tr>

        <tr>
            <td># Kert</td>

            <td>1</td>

            <td>39:59</td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>31</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0.0</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td></td>
        </tr>

        <tr>
            <td># Koit</td>

            <td>1</td>

            <td>39:59</td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>31</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0.0</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td></td>
        </tr>

        <tr>
            <td># KƤrt</td>

            <td>1</td>

            <td>39:59</td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>31</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0.0</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td></td>
        </tr>

        <tr>
            <td># MƤrt</td>

            <td>1</td>

            <td>39:59</td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>31</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0.0</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td></td>
        </tr>

        <tr>
            <td># Ćlar</td>

            <td>1</td>

            <td>39:59</td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>0/0</td>

            <td></td>

            <td>31</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0.0</td>
        </tr>

        <tr>
            <td></td>
        </tr>

        <tr>
            <td class="headerStyle" style="font-weight: bold">Vastased</td>

            <td class="headerStyle" style="font-weight: bold">1</td>

            <td class="headerStyle" style="font-weight: bold"></td>

            <td class="headerStyle" style="font-weight: bold">37/71</td>

            <td class="headerStyle" style="font-weight: bold">52,1%</td>

            <td class="headerStyle" style="font-weight: bold">32/52</td>

            <td class="headerStyle" style="font-weight: bold">61,5%</td>

            <td class="headerStyle" style="font-weight: bold">5/19</td>

            <td class="headerStyle" style="font-weight: bold">26,3%</td>

            <td class="headerStyle" style="font-weight: bold">9/26</td>

            <td class="headerStyle" style="font-weight: bold">34,6%</td>

            <td class="headerStyle" style="font-weight: bold"></td>

            <td class="headerStyle" style="font-weight: bold">40</td>

            <td class="headerStyle" style="font-weight: bold">18</td>

            <td class="headerStyle" style="font-weight: bold">22</td>

            <td class="headerStyle" style="font-weight: bold">4</td>

            <td class="headerStyle" style="font-weight: bold">15</td>

            <td class="headerStyle" style="font-weight: bold">4</td>

            <td class="headerStyle" style="font-weight: bold">1</td>

            <td class="headerStyle" style="font-weight: bold">7</td>

            <td class="headerStyle" style="font-weight: bold"></td>

            <td class="headerStyle" style="font-weight: bold">16</td>

            <td class="headerStyle" style="font-weight: bold">88</td>

            <td class="headerStyle" style="font-weight: bold">
            0.5714285714285714</td>
        </tr>

        <tr>
            <td></td>
        </tr>

        <tr>
            <td class="headerStyle" style="font-weight: bold">Player</td>

            <td class="headerStyle" style="font-weight: bold">GAMES</td>

            <td class="headerStyle" style="font-weight: bold">MIN</td>

            <td class="headerStyle" style="font-weight: bold">FG</td>

            <td class="headerStyle" style="font-weight: bold">FG%</td>

            <td class="headerStyle" style="font-weight: bold">2P</td>

            <td class="headerStyle" style="font-weight: bold">2P%</td>

            <td class="headerStyle" style="font-weight: bold">3P</td>

            <td class="headerStyle" style="font-weight: bold">3P%</td>

            <td class="headerStyle" style="font-weight: bold">FT</td>

            <td class="headerStyle" style="font-weight: bold">FT%</td>

            <td class="headerStyle" style="font-weight: bold">+/-</td>

            <td class="headerStyle" style="font-weight: bold">REB</td>

            <td class="headerStyle" style="font-weight: bold">OREB</td>

            <td class="headerStyle" style="font-weight: bold">DREB</td>

            <td class="headerStyle" style="font-weight: bold">AST</td>

            <td class="headerStyle" style="font-weight: bold">STL</td>

            <td class="headerStyle" style="font-weight: bold">DEFL</td>

            <td class="headerStyle" style="font-weight: bold">BLK</td>

            <td class="headerStyle" style="font-weight: bold">TO</td>

            <td class="headerStyle" style="font-weight: bold">EFF</td>

            <td class="headerStyle" style="font-weight: bold">PF</td>

            <td class="headerStyle" style="font-weight: bold">PTS</td>

            <td class="headerStyle" style="font-weight: bold">AST/TO</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td></td>
        </tr>

        <tr>
            <td>#null null</td>

            <td>1</td>

            <td>0:00</td>

            <td>1/1</td>

            <td>100%</td>

            <td>1/1</td>

            <td>100%</td>

            <td>0/0</td>

            <td></td>

            <td>1/2</td>

            <td>50%</td>

            <td>0</td>

            <td>3</td>

            <td>0</td>

            <td>3</td>

            <td>0</td>

            <td>0</td>

            <td>1</td>

            <td>0</td>

            <td>2</td>

            <td>3</td>

            <td>4</td>

            <td>3</td>

            <td>0.0</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td></td>
        </tr>

        <tr>
            <td>#4 Siim Punab</td>

            <td>1</td>

            <td>33:04</td>

            <td>4/8</td>

            <td>50%</td>

            <td>3/5</td>

            <td>60%</td>

            <td>1/3</td>

            <td>33,3%</td>

            <td>2/3</td>

            <td>66,7%</td>

            <td>-28</td>

            <td>3</td>

            <td>0</td>

            <td>3</td>

            <td>2</td>

            <td>0</td>

            <td>1</td>

            <td>0</td>

            <td>2</td>

            <td>9</td>

            <td>1</td>

            <td>11</td>

            <td>1.0</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td></td>
        </tr>

        <tr>
            <td>#5 Janar Lauren</td>

            <td>1</td>

            <td>30:33</td>

            <td>0/4</td>

            <td>0%</td>

            <td>0/2</td>

            <td>0%</td>

            <td>0/2</td>

            <td>0%</td>

            <td>0/0</td>

            <td></td>

            <td>-35</td>

            <td>2</td>

            <td>0</td>

            <td>2</td>

            <td>3</td>

            <td>2</td>

            <td>0</td>

            <td>0</td>

            <td>3</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>1.0</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td></td>
        </tr>

        <tr>
            <td>#6 Ilmar Kilg</td>

            <td>1</td>

            <td>33:29</td>

            <td>2/8</td>

            <td>25%</td>

            <td>2/7</td>

            <td>28,6%</td>

            <td>0/1</td>

            <td>0%</td>

            <td>0/0</td>

            <td></td>

            <td>-29</td>

            <td>2</td>

            <td>0</td>

            <td>2</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>5</td>

            <td>-5</td>

            <td>3</td>

            <td>4</td>

            <td>0.0</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td></td>
        </tr>

        <tr>
            <td>#8 Martin Ligi</td>

            <td>1</td>

            <td>36:32</td>

            <td>2/5</td>

            <td>40%</td>

            <td>2/5</td>

            <td>40%</td>

            <td>0/0</td>

            <td></td>

            <td>5/6</td>

            <td>83,3%</td>

            <td>-49</td>

            <td>4</td>

            <td>3</td>

            <td>1</td>

            <td>1</td>

            <td>1</td>

            <td>0</td>

            <td>0</td>

            <td>6</td>

            <td>5</td>

            <td>3</td>

            <td>9</td>

            <td>0.16666666666666666</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td></td>
        </tr>

        <tr>
            <td>#13 Avo PƵldma</td>

            <td>1</td>

            <td>21:37</td>

            <td>0/4</td>

            <td>0%</td>

            <td>0/3</td>

            <td>0%</td>

            <td>0/1</td>

            <td>0%</td>

            <td>0/0</td>

            <td></td>

            <td>-25</td>

            <td>5</td>

            <td>0</td>

            <td>5</td>

            <td>0</td>

            <td>2</td>

            <td>0</td>

            <td>0</td>

            <td>5</td>

            <td>-2</td>

            <td>3</td>

            <td>0</td>

            <td>0.0</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td></td>
        </tr>

        <tr>
            <td>#14 Janek Heitur</td>

            <td>1</td>

            <td>21:28</td>

            <td>0/6</td>

            <td>0%</td>

            <td>0/2</td>

            <td>0%</td>

            <td>0/4</td>

            <td>0%</td>

            <td>0/2</td>

            <td>0%</td>

            <td>-32</td>

            <td>1</td>

            <td>0</td>

            <td>1</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>2</td>

            <td>-9</td>

            <td>4</td>

            <td>0</td>

            <td>0.0</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td></td>
        </tr>

        <tr>
            <td>#32 Erki Saar</td>

            <td>1</td>

            <td>23:12</td>

            <td>2/6</td>

            <td>33,3%</td>

            <td>2/4</td>

            <td>50%</td>

            <td>0/2</td>

            <td>0%</td>

            <td>0/4</td>

            <td>0%</td>

            <td>-27</td>

            <td>2</td>

            <td>1</td>

            <td>1</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>0</td>

            <td>4</td>

            <td>-6</td>

            <td>1</td>

            <td>4</td>

            <td>0.0</td>
        </tr>

        <tr>
            <td></td>
        </tr>

        <tr>
            <td class="headerStyle" style="font-weight: bold">TĆ/Rock
            Fanclub</td>

            <td class="headerStyle" style="font-weight: bold">1</td>

            <td class="headerStyle" style="font-weight: bold"></td>

            <td class="headerStyle" style="font-weight: bold">11/42</td>

            <td class="headerStyle" style="font-weight: bold">26,2%</td>

            <td class="headerStyle" style="font-weight: bold">10/29</td>

            <td class="headerStyle" style="font-weight: bold">34,5%</td>

            <td class="headerStyle" style="font-weight: bold">1/13</td>

            <td class="headerStyle" style="font-weight: bold">7,7%</td>

            <td class="headerStyle" style="font-weight: bold">8/17</td>

            <td class="headerStyle" style="font-weight: bold">47,1%</td>

            <td class="headerStyle" style="font-weight: bold"></td>

            <td class="headerStyle" style="font-weight: bold">22</td>

            <td class="headerStyle" style="font-weight: bold">4</td>

            <td class="headerStyle" style="font-weight: bold">18</td>

            <td class="headerStyle" style="font-weight: bold">6</td>

            <td class="headerStyle" style="font-weight: bold">5</td>

            <td class="headerStyle" style="font-weight: bold">2</td>

            <td class="headerStyle" style="font-weight: bold">0</td>

            <td class="headerStyle" style="font-weight: bold">29</td>

            <td class="headerStyle" style="font-weight: bold"></td>

            <td class="headerStyle" style="font-weight: bold">19</td>

            <td class="headerStyle" style="font-weight: bold">31</td>

            <td class="headerStyle" style="font-weight: bold">
            0.20689655172413793</td>
        </tr>
    </table>

    <table width="100%">
        <tr>
            <td class="headerStyle" colspan="2" style="font-weight: bold">Stat
            Key</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td class="headerStyle">PTS</td>

            <td>Points: Associated with a player or team.</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td class="headerStyle">FG/FGA</td>

            <td>Ratio of Field Goals made to Field Goals attempted.</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td class="headerStyle">FG%</td>

            <td>Percentage of Field Goals made to Field Goals attempted.</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td class="headerStyle">3PM/3PA</td>

            <td>Ratio of 3 Point Field Goals made to 3 Point Field Goals
            attempted.</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td class="headerStyle">3P%</td>

            <td>Percentage of 3 Point Field Goals made to 3 Point Field Goals
            attempted.</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td class="headerStyle">FTM/FTA</td>

            <td>Ratio of Free Throws made to Free Throws attempted.</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td class="headerStyle">FT%</td>

            <td>Percentage of Free Throws made to Free Throws attempted.</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td class="headerStyle">REB</td>

            <td>Total Rebounds, which includes Offensive and Defensive
            rebounds.</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td class="headerStyle">OREB</td>

            <td>Offensive Rebounds, where a offensive player successfully gains
            possession of the ball after a missed shot attempt.</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td class="headerStyle">DREB</td>

            <td>Defensive Rebounds, where a defensive player successfully gains
            possession of the ball after a missed shot attempt.</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td class="headerStyle">AST</td>

            <td>Assists, where an offensive player passes a ball that results
            in a field goal.</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td class="headerStyle">STL</td>

            <td>Steals, where a defensive player causes a turn over by their
            aggressive action.</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td class="headerStyle">BLK</td>

            <td>Blocked Shots, where a defensive player legally deflects a
            field goal attempt.</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td class="headerStyle">DEF</td>

            <td>Defensive Deflections, where a defensive player deflects a ball
            but possession is not changed.</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td class="headerStyle">TO</td>

            <td>Turnovers, where an offensive player loses possession to a
            defensive player.</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td class="headerStyle">EFF</td>

            <td>Efficiency, statistical benchmark for comparing the overall
            value of a player calculated as (PTS+REB+AST+STL+BLK-FG missed-FT
            missed-TO).</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td class="headerStyle">+/-</td>

            <td>The +/- stat represents team performance while a player is in
            the game. If the home team hits a 2 pointer this would make the +/-
            for the checked in players on the home team havea +2 and the
            checked in players for the away team have a -2</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td class="headerStyle">PF</td>

            <td>Personal Fouls, is a breach of the rules that concerns illegal
            personal contact with an opposing player.</td>
        </tr>

        <tr style="background-color: #DDDDDD">
            <td class="headerStyle">PF</td>

            <td>Charge Drawn: when a defensive player keeps and establishes
            position and is fouled by an offensive player.</td>
        </tr>

        <tr style="background-color: #EEEEEE">
            <td class="headerStyle">AST/TO</td>

            <td>Ratio of Assists to Turnovers.</td>
        </tr>
    </table>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您的multiDim语法/逻辑对我很有用。

function multiDim($array){
    foreach($array as $key => $value){
        $array[$key] = is_array($value) ? multiDim($value) : trim($value);
    }
    return $array;
}

这可能会导致多个参考文献的潜在复杂性。试一试。