...

View Full Version : What's wrong with this foreach & array ??



DoA
03-20-2010, 01:00 AM
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


<?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;
}
?>

koko5
03-20-2010, 01:10 AM
<?php
...............
foreach($arr as $key => $value);
...............
?>
Remove ; and should works :thumbsup:

DoA
03-20-2010, 01:13 AM
Hi koko5,

Thanks for that ... it's nearly there but perhaps I'm using the wrong loop.

It's now outputting



Not FoundNot Found2Not Found


I know why, because it's checking each array value ... but I only want it to output 3 and not the Not Found 'errors' for the other array items.....

koko5
03-20-2010, 01:16 AM
....but I only want it to output 3 and not the Not Found 'errors' for the other array items.....
Actually it outputs 2 because array starts from [0] element:

$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";
}
}
echo $Position."\n";
Regards :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum