0

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]=>[""]
  } 
  }   
...
BarclayVision
  • 865
  • 2
  • 12
  • 41
  • You should loop over the `tr` elements, not `td.Chats`. The first `td` in the `tr` becomes the name. Then loop over the remaining `td` elements in the row to get the times and colors. – Barmar Sep 20 '16 at 16:41
  • Why do you have `array(1)` for arrays that have two elements, `time` and `color`.? – Barmar Sep 20 '16 at 16:43
  • corrected question, the results will be blank if no time exist, see new change - also see original post on this topic: http://stackoverflow.com/questions/39574672/screen-scraping-php-using-simplehtmldom – BarclayVision Sep 20 '16 at 16:45
  • @barmar2 - I'm not sure, that is what was generated by the simplehtmldom library as results? – BarclayVision Sep 20 '16 at 16:46
  • 1
    I meant in your desired results, you have `array(1) { [time]=>[""], [color[=>[""] }`. That should be `array(2)`. You understand that the number in parentheses is the number of elements in the array, don't you? – Barmar Sep 20 '16 at 16:48
  • I don't understand your notation. You can't have two pointers like `["xxx"] => ["yyy"] => array()`. `=>` is used for the key-value relationship in an array, but there's no array for the second `=>`. – Barmar Sep 20 '16 at 16:53
  • @barmar In your first comment you recommended loop `tr` but how do I set element name to `name`? – BarclayVision Sep 20 '16 at 16:53
  • Do you want `array('name' => "Anne Arundel", 'time' => [""], 'color' => [""])`? – Barmar Sep 20 '16 at 16:54
  • I need to every name (hosp. name) and get time and color from every name if it exist. End results will be JSON, but need a properly formated array first... – BarclayVision Sep 20 '16 at 16:55
  • yes that would work as well – BarclayVision Sep 20 '16 at 16:55
  • Or maybe you want the top-level array to be `array('Anne Arundel' => array('time' => [""], 'color' => [""]), 'Baltimore Washington Medical Center' => array('time' => ["04:31"], 'color' => "background-color:#ffff00;color:#000000;"))` – Barmar Sep 20 '16 at 16:56

1 Answers1

1

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>';
Barmar
  • 741,623
  • 53
  • 500
  • 612