如何从数组中提取子数据
How to extract sub data out of arrays
我在 PHP 中使用 SIMPLE HTML DOM Scraper 试图获得各种运动队的一些统计数据
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
我想从中得到的是团队,然后是统计数据,例如索引 0 几乎有 5 个子数组索引。
[0] = green bay
[1] = 4-0-0
[2] = 100%
[3] = 12.8
[4] = +10.9
您可以看到主数组中有多个元素需要执行此操作。什么是最好的方法,或者我应该用刮刀以不同的方式来做?
获取 ->children()
,这将是 td
,然后遍历它们以获得 ->text()
,可以使用 foreach 或 array_map 等
<?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
)
*/
我在 PHP 中使用 SIMPLE HTML DOM Scraper 试图获得各种运动队的一些统计数据
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
我想从中得到的是团队,然后是统计数据,例如索引 0 几乎有 5 个子数组索引。
[0] = green bay
[1] = 4-0-0
[2] = 100%
[3] = 12.8
[4] = +10.9
您可以看到主数组中有多个元素需要执行此操作。什么是最好的方法,或者我应该用刮刀以不同的方式来做?
获取 ->children()
,这将是 td
,然后遍历它们以获得 ->text()
,可以使用 foreach 或 array_map 等
<?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
)
*/