doa24uk Posted March 19, 2010 Share Posted March 19, 2010 As you can see, the Target is 3.com and it IS in the array ... so why isn't this working?? It's continuously outputting Not Found :shrug: :wtf: <?php $Target="3.com"; $arr=array("http://1.com","http://2.com","http://www.3.com/whatever","http://4.com"); foreach($arr as $key => $value); { if(stristr($value, $Target)) { $Position = $key; } else { $Position = "Not Found"; } echo $Position; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/195868-whats-wrong-with-this-foreach-array/ Share on other sites More sharing options...
fr34k Posted March 19, 2010 Share Posted March 19, 2010 You shouldn't have the semicolon after your foreach. Also, you need a break. $Target="3.com"; $arr=array("http://1.com","http://2.com","http://www.3.com/whatever","http://4.com"); foreach($arr as $key => $value) { if(stristr($value, $Target)) { $Position = $key; break; } else $Position = "Not Found"; } print $Position; Quote Link to comment https://forums.phpfreaks.com/topic/195868-whats-wrong-with-this-foreach-array/#findComment-1028826 Share on other sites More sharing options...
doa24uk Posted March 19, 2010 Author Share Posted March 19, 2010 Thanks, just expanding this slightly - that loop is actually within another loop..... $Target = "phrase 2" $textarea_array=array("phrase 1","phrase 2"); foreach ($textarea_array as $XQuery) { $html = "http://www.mysite.co.uk/search?api=" .$XQuery; $dom = new DOMDocument; @$dom->loadHTMLFile($html); $xpath = new DOMXPath($dom); $aTag = $xpath->query('//h3[@class="r"]/a'); foreach ($aTag as $val) { $arr[] = $val->getAttribute('href'); } foreach($arr as $key => $value) { if(stristr($value, $Target)) { $Position = $key; break; } else { $Position = "Not Found"; } } echo $XQuery; echo "-"; echo $Position; echo "<br><br>"; } What should be being output is phrase 1 - position of phrase 1 phrase 2 - position of phrase 2 What is actually being output is phrase 1 - position of phrase 1 phrase 2 - position of phrase 1 And I just can see why!? Quote Link to comment https://forums.phpfreaks.com/topic/195868-whats-wrong-with-this-foreach-array/#findComment-1028834 Share on other sites More sharing options...
fr34k Posted March 20, 2010 Share Posted March 20, 2010 I'm assuming that's sanitized and you aren't posting the true code. That makes it a little more difficult to diagnose, because the values you're passing could be causing an issue. The site that you're posting to could be returning data that's causing the problem. Nothing is jumping out at me, with the code you posted. Can you post the real code? Quote Link to comment https://forums.phpfreaks.com/topic/195868-whats-wrong-with-this-foreach-array/#findComment-1028844 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.