Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    DoA
    DoA is offline
    New Coder
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    What's wrong with this foreach & array ??

    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

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

  • #2
    New Coder
    Join Date
    Mar 2009
    Posts
    89
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by DoA View Post
    Code:
    <?php
    ...............
    foreach($arr as $key => $value);
    ...............
    ?>
    Remove ; and should works
    URL2SEO URL shortener and redirection services

  • #3
    DoA
    DoA is offline
    New Coder
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi koko5,

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

    It's now outputting

    Code:
    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.....

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    89
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by DoA View Post
    ....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:
    PHP Code:
    $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
    URL2SEO URL shortener and redirection services


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •