dqlm80253 2019-02-15 22:47
浏览 39
已采纳

PHP:HTML到PHP多维关联数组

Using xPath query, I am trying to extract HTML values and put them into an associative array in PHP. I am using a loop to get the rows and cells from the table. But, I can;t figure out how to get the cells in an array embedded within an array that represents the row. Basically, just transferring the table structure to an array. Ideally, it would help to assign keys for the cell data.

I tried combining $key as an array and a counter to assign key/value pair. I and xpath at different points of the structure. I can get fill up the array but I am But I just can't seem to crack it.

$cells = array();
$cell_values = array();
$key = array("MM", "DD", "TIME", "WVHT", "SwH", "SwP", "SwD", "WWH", "WWP", "WWD", "STEEPNESS", "APD");
$i = 3;

while($i <= 5){
    $rows = $xpath->query('//table[@class="dataTable"][2]/tr['.$i.']');
    if (!is_null($rows)){
        foreach ($rows as $row) {
            $cells = $row->getElementsByTagName('td');
            $i++;
            foreach ($cells as $cell) {
                $cell_values[] = $cell->nodeValue;
                $dataOut[] = array_combine($key, $cell_values);
            }
        }   
    }   
}

Expected:

Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] 
=> [10] => 
[11] => Array ( [MM] => 02 [DD] => 17 [TIME] => 11:30 am [WVHT] 
=> 3.0 [SwH] => 0.3 [SwP] => 10.5 [SwD] => SE [WWH] => 2.6 [WWP] => 8.3 
[WWD] => SE [STEEPNESS] => AVERAGE [APD] => 4.4 ) 
//Next set of row data with $keys
[12] => Array ( [MM] => 02 [DD] => 17 [TIME] => 11:00 am [WVHT] => 3.3 [SwH] 
=> 0.3 [SwP] => 10.5 [SwD] => SE [WWH] => 2.6 [WWP] => 8.3 [WWD] => SE 
[STEEPNESS] => AVERAGE [APD] => 4.4 ) 
[13] => Array... etc. 

What I Get:

Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] 
=> [10] => [11] => Array ( [MM] => 02 [DD] => 17 [TIME] => 11:30 am [WVHT] 
=> 3.0 [SwH] => 0.3 [SwP] => 10.5 [SwD] => SE [WWH] => 2.6 [WWP] => 8.3 
[WWD] => SE [STEEPNESS] => AVERAGE [APD] => 4.4 ) [12] => [13] => [14] => 
[15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] 
=> [25] => [26] => [27] => [28] => [29] => [30] => [31] => [32] => [33] => 
[34] => [35] => )
  • 写回答

1条回答 默认 最新

  • dongxing4643 2019-02-15 23:30
    关注

    If you define your keys as values in the $key array rather than keys, you can array_combine that with the values from the <td>s to produce the rows for your result array.

    $rows = $xpath->query('//table[@class="dataTable"][2]/tr');
    
    // define these as values so you can use them in array_combine    
    $key = array("WVHT", "SwH", "SwD", "WWH", "WWP", "WWD", "STEEPNESS", "AMD");
    
    $data = array();
    $cells = array();
    
    if (!is_null($rows)) {
        foreach ($rows as $row) {
            $cells = $row->getElementsByTagName('td');
    
            // get all the cell values from the row
            $row_values = [];
            foreach ($cells as $cell) {
                $row_values[] = $cell->nodeValue;
            }
    
            // combine the keys with the values for this row and add the row to $data
            $data[] = array_combine($key, $row_values);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?