Arrays merging in foreach loops and shouldn't then searching for a string
in arrays fails the second iteration
I have the code below within a while loop, which is looping over html
files. For each file it extracts the <li> tags and from these I get the
header 2 <a> tags. It then searches each array $node which contains the
<a> tags for the string and outputs in the if condition. The problem is
when I echo the $node in the lower foreach loop I see that all <a> tags
have been combined in to one large loop.
The first iteration has the correct amount which are all the <a> tags from
a single file. The string is found for this loop. But other strings are
not found for the other loops, $string is dynamic and changes with each
loop, i've echoed it and see it's correct in each loop. I think I have to
reset $test or $all_a_nodes and have tried putting reset(), and unset()
all over my code with no luck, I've also tried, break, and continue but
just can't figure it out.
When I echo $result in the first loop I see two sets of <li> tags which is
correct, now within the second for each loop I echo $node, the first loop
has the first set of a tags from the first file which is fine and the
string is found, it's the second third fourth iteration that these start
to combine and no string is found.
I'm lost on what to change here and would really appreciate any help
Thanks!
foreach($ret as $result){
if ($result->tag=='li') {
echo $result;
foreach($result->find('h2 a') as $a)
{
$test .= $a.',';
}
}
}
$html = str_get_html($test);
$string = $hostarray['search'];
$all_a_nodes = $html->find('a');
$d = 0;
$data = array();
foreach( $all_a_nodes as $node ) {
echo $node;
echo $string;
if( strpos( (string)$node, $string ) === false ) {
echo 'string not found in link ' . $d ;
} else {
echo 'found'.$d;
}
}
No comments:
Post a Comment