doute7910 2016-09-20 16:03
浏览 37
已采纳

使用SimpleHtmlDom的数组格式[关闭]

Need to change the output of the array format generated by SimpleHtmlDom. my PHP code is. The results of the SimpleHtmlDom that I'm using is returning hospital names as the key not the value?:

<?php
require('simple_html_dom.php');

$table = array();
$html = file_get_html('https://www.miemssalert.com/chats/Default.aspx?hdRegion=3');

foreach($html->find('table#tblHospitals tr td.Chats') as $e)
{
    //echo $e->plaintext . $e->getAttribute('style') . '<hr>';
$nametime = $e->plaintext;
$color = $e->getAttribute('style');
$table[$nametime][$color] = true;
}
echo json_encode($table);

echo '<pre>';
    var_dump($table);
echo '</pre>';
?>

current Array results:

array(37) {
  ["Anne Arundel Medical Center"]=>
  array(1) {
    [0]=>
    bool(true)
  }
  [""]=>
  array(1) {
    [0]=>
    bool(true)
  }
  ["Baltimore Washington Medical Center"]=>
  array(1) {
    [0]=>
    bool(true)
  }
  ["04:31"]=>
  array(1) {
    ["background-color:#ffff00;color:#000000;"]=>
    bool(true)
  }
  ["Bon Secours Hospital"]=>
  array(1) {
    [0]=>
    bool(true)
  }
...

Looking for results to be nested array by Name=>Time=>Color

array(37) {
  array(1) {["Name"]=>["Anne Arundel Medical Center"]=>
  array(2) {
    [time]=>[""],[color]=>[""]
  }
  } 
  array(1) {["Name"]=>["Baltimore Washington Medical Center"]=>
  array(2) {
    [time]=>["04:31"],[color]=>["background-color:#ffff00;color:#000000;"]
  } 
  }
  array(1) {["Name"]=>["Bon Secours Hospital"]=>
  array(2) {
    [time]=>[""],[color]=>[""]
  } 
  }   
...
  • 写回答

1条回答 默认 最新

  • dongpeng7744 2016-09-20 17:00
    关注

    You need to loop over the rows, not the cells, so that each hospital corresponds to an element in the result array. Get the hospital name from the first td in the row, and the times and colors from the remaining ones in a nested loop.

    <?php
    require('simple_html_dom.php');
    
    $table = array();
    $html = file_get_html('https://www.miemssalert.com/chats/Default.aspx?hdRegion=3');
    
    foreach($html->find('table#tblHospitals tr') as $hosp)
        {
            $tds = $hosp->find('td.Chats');
            if (!empty($tds)) {
                $name = $tds[0]->plaintext;
                $row = array('name' => $name, 'time' => array(), 'color' => array());
                foreach (array_slice($tds, 1) as $e) {
                    $time = $e->plaintext;
                    $color = $e->getAttribute('style');
                    $row['time'][] = $time;
                    $row['color'][] = $color;
                }
                $table[] = $row;
            }
        }
    
    echo '<pre>';
    var_dump($table);
    echo '</pre>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么