Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从数组中提取子数据_Php_Simple Html Dom - Fatal编程技术网

Php 如何从数组中提取子数据

Php 如何从数组中提取子数据,php,simple-html-dom,Php,Simple Html Dom,我在PHP中使用简单的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(); 返回: Array ( [0] =

我在PHP中使用简单的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();

    
返回:

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或数组映射等

<?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
        )
 */

看起来这些只是字符串。因此,如果你使用空格字符作为分隔符来分解它们,你应该得到你需要的东西,我认为。@lawrencerone比我的答案更好,我没有去看实际被刮的页面/没有推断出明显的表格单元格。哇,谢谢你,这正是我想要的。现在我的下一个挑战是获取数据。我会做一个foreach($rows as$row){$team=$row[0];}np感谢您的接受,这就像是
foreach($team as$rows){//对$team[0]做一些事情,它将是绿湾,$team[1]将是4-0-0}