I am using SIMPLE HTML DOM Scraper in PHP trying to get some statistics for various sports teams
index.php
$html = file_get_html("https://www.teamrankings.com/nfl/trends/ats_trends/"); foreach($html->find("tbody tr") as $h){ $rows[] = $h->text();
Returns:
Array ( [0] => Green Bay 4-0-0 100.0% 12.8 +10.9 [1] => LA Chargers 4-1-0 80.0% -3.0 -0.1 [2] => Seattle 4-1-0 80.0% 6.8 +2.9 [3] => Pittsburgh 3-1-0 75.0% 7.8 +2.0
What im trying to get out of this is the team, followed by stats, for example index 0 would almost have 5 sub array indexs.
[0] = green bay [1] = 4-0-0 [2] = 100% [3] = 12.8 [4] = +10.9
You can see there are multiple elements in the main array where I would need to do this. What is the best way, or should I be doing it a different way with the scraper?
Advertisement
Answer
Get the ->children()
which would be the td
‘s then loop over them to get ->text()
, could use a foreach or array_map etc.
<?php include 'simple_html_dom.php'; $html = file_get_html("https://www.teamrankings.com/nfl/trends/ats_trends/"); $rows = []; foreach($html->find("tbody tr") as $tr) { $rows[] = array_map(function($td) { return trim($td->text()); }, $tr->children()); } print_r($rows); /** * Array ( [0] => Array ( [0] => Green Bay [1] => 4-0-0 [2] => 100.0% [3] => 12.8 [4] => +10.9 ) [1] => Array ( [0] => LA Chargers [1] => 4-1-0 [2] => 80.0% [3] => -3.0 [4] => -0.1 ) */