I am trying to use simple html dom to parse my html data. But whenever i use the below code i get error as :-
Trying to get property of non-object
My code is:-
foreach($html->find('div.row') as $val)
{
$name = $val->find('h1')->innertext;
echo $name;
}
But when i use it like this i get the proper result:-
foreach($html->find('div.row') as $val)
{
foreach($val->find('h1') as $v)
echo $v->innertext;
}
The find() method returns an array unless you specify an index as the second argument. Your second piece of code works as it loops through the array and gets the innertext of each element in the array, whereas your first piece of code is trying to get the innertext property of the actual array.
If you want to get the first (or only) occurrence of h1 for example, do this:
$name = $val->find('h1', 0)->innertext;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With