错误:“尝试获取非对象的属性”和无效的foreach循环参数

I want to use simple HTML DOM parser to parse the HTML of a table. I want to put <td> tags of each <tr> in an array, and then put those arrays in a bigger array.

I was following the code given here as a reference.

But when I first tested this code as it is, I got this error:

Notice: Trying to get property of non-object in C:\xampp\htdocs\tests.php on line 42

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\tests.php on line 44

Please tell me why am I getting this error, and how to fix it.

The code is as follows:

<?php

$html_string = 'Edit question</a></div></div><div class="content"><div class="formulation"><h4 class="accesshide">Question text</h4><input type="hidden" name="q18:1_:sequencecheck" value="1" /><div class="qtext"><table style="width: 454px; height: 269px;" border="1"><caption> </caption>
<tbody>
<tr>
<td>Name</td>
<td>Age</td>
<td>CGPA</td>
</tr>
<tr>
<td>Alice</td>
<td>24</td>
<td>4</td>
</tr>
<tr>
<td>Bob</td>
<td>14</td>
<td>3</td>
</tr>
<tr>
<td>Amy</td>
<td>33</td>
<td>2</td>
</tr>
</tbody>
</table>
<p> </p>
<p>Blah BlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlah BlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlah?</p></div><div class="ablock"><div class="prompt">Select one:</div><div class="answer"><div class="r0"><input type="radio" name="q18:1_answer" value="0" id="q18:1_answer0" /><label for="q18:1_answer0">a. [1]ir[/1][2]34[/2]</label> </div>';

$dom = new domDocument;

@$dom->loadHTML($data);
$dom->preserveWhiteSpace = false;

$tables = $dom->getElementsByTagName('table');

echo get_inner_html($tables->item(0));

function get_inner_html( $node ) 
{
    $innerHTML= '';
    $children = $node->childNodes;

    foreach ($children as $child)
    {
        $innerHTML .= $child->ownerDocument->saveXML( $child );
    }

    return $innerHTML;
}

You have another variable name, $data, in the loadHTML, change it to @$dom->loadHTML($html_string);