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);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问