Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.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 如何使用SimpleHTMLDom在数组中正确获取此表_Php_Html_Arrays_Loops_Simple Html Dom - Fatal编程技术网

Php 如何使用SimpleHTMLDom在数组中正确获取此表

Php 如何使用SimpleHTMLDom在数组中正确获取此表,php,html,arrays,loops,simple-html-dom,Php,Html,Arrays,Loops,Simple Html Dom,首先,我想告诉你们我为这个问题寻找解决方案已经有多长时间了,但我就是找不到 问题 我使用SimpleHTMLDom获取一个表并将其排序为一个数组。乍一看,我以为这很容易,但不是 首先是我的HTML,您可以在这里找到它: 我的PHP将其放入数组: public function fetchContent() { $first = new \Bavix\AdvancedHtmlDom\AdvancedHtmlDom(); $html = $first-&g

首先,我想告诉你们我为这个问题寻找解决方案已经有多长时间了,但我就是找不到

问题
我使用SimpleHTMLDom获取一个表并将其排序为一个数组。乍一看,我以为这很容易,但不是

首先是我的HTML,您可以在这里找到它:

我的PHP将其放入数组:

public function fetchContent()
    {

        $first = new \Bavix\AdvancedHtmlDom\AdvancedHtmlDom();
        $html = $first->loadFile($this->DynamicLink);
        $trs = $html->find('table', 0)->children();

        //Defining needed variables
        $lastOneRowspan = false;
        $contentOfTr = '';
        for ($i = 0; $i < 6; $i++) {
            $count = 0;
            $content = [];
            $number = $i;
            foreach ($trs as $tr) {
                if ($count % 2 !== 0) {
                    $lesson = $tr->children($number);
                    $lessonContent = $lesson->plaintext();

                    if ($lastOneRowspan) {
                        $content[] = $contentOfTr;
                        $content[] = $lessonContent;
                    } else {
                        $content[] = $lessonContent;
                    }
                    
                    if ($lesson['rowspan'] > 2) {
                        $lastOneRowspan = true;
                        $contentOfTr = $lessonContent;
                    } else {
                        $lastOneRowspan = false;
                    }
                }
                $count++;
            }
            $finalContent[] = $content;
        }
        return $finalContent;
    }
我就快到了,但有一个问题我无法解决。在TD的行跨度为4的TR之后的TR将少一个TD。因此,当它真的需要获得第四个td时,它将获得第五个td。您可以在php生成的数组中的第4个数组中看到这一点。在该数组中,[2]等于'-3',但它必须等于空TD。正如您在HTML中看到的那样。我试过很多东西。但其本质是,它需要先抓住一个,再抓住一个

我希望我能清楚地描述我的问题。如果你有任何问题,请告诉我

无论如何,提前谢谢你的帮助

 Array
(
    [0] => Array
        (
            [0] => 
1
 

8:30
 

9:20
 

            [1] => 
2
 

9:20
 

10:10
 

            [2] => 
3
 

10:25
 

11:15
 

            [3] => 
4
 

11:15
 

12:05
 

            [4] => 
5
 

12:05
 

12:55
 

            [5] => 
6
 

12:55
 

13:45
 

            [6] => 
7
 

13:45
 

14:35
 

            [7] => 
8
 

14:50
 

15:40
 

            [8] => 
9
 

15:40
 

16:30
 

            [9] => 
10
 

16:30
 

17:20
 

        )

    [1] => Array
        (
            [0] => 
 
 

            [1] => 
 
 

            [2] => 
 
 

            [3] => 
 
 

            [4] => 
 
 

            [5] => 
 
 

            [6] => 
 
 

            [7] => 
 
 

            [8] => 
 
 

            [9] => 
 
 

        )

    [2] => Array
        (
            [0] => 
            [1] => 
            [2] => 
BRANE0
 

BURG
 

            [3] => 
HAKES0
 

ENG
 

            [4] => 
HORSH0
 

REK
 

            [5] => 
HAKES0
 

PROF
 

            [6] => 
            [7] => 
            [8] => 
            [9] => 
        )

    [3] => Array
        (
            [0] => 
            [1] => 
DIJKS0.
 

ALK F021
 

ONTW GER ACT 1
 

ASLAN0
 

ALK E004
 

ONTW GER ACT
 

            [2] => 
DIJKS0.
 

ALK F021
 

ONTW GER ACT 1
 

ASLAN0
 

ALK E004
 

ONTW GER ACT
 

            [3] => 
            [4] => 
HOEKM0
 

ALK E025
 

AGO 6
 

            [5] => 
HIELJ0
 

ALK E025
 

AGO 5
 

            [6] => 
HAKES0
 

ALK E025
 

LOB GROEP
 

            [7] => 
HAKES0
 

ALK E223
 

SLOB
 

            [8] => 
            [9] => 
            [10] => 
        )

    [4] => Array
        (
            [0] => 
            [1] => 
            [2] => 
-3
 

            [3] => 
            [4] => 
WITDE0.
 

NED
 

            [5] => 
WITDE0.
 

NED
 

            [6] => 
-3
 

            [7] => 
            [8] => 
            [9] => 
            [10] => 
        )

    [5] => Array
        (
            [0] => 
-3
 

            [1] => 
-3
 

            [2] => 
-3
 

            [3] => 
-3
 

            [4] => 
-3
 

            [5] => 
-3
 

            [6] => 
-3
 

            [7] => 
-3
 

            [8] => 
-3
 

            [9] => 
-3
 

        )

)