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 5 of 5
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts

    How do I check the contents of an array for my string?

    Hi,

    I a string that is coming from my database table say $needle.
    If te needle is not in my array, then I want to add it to my array.

    If it IS in my array then so long as it is in only twice, then I still
    want to add it to my array (so three times will be the maximum)

    In order to check to see is if $needle is in my $haystack array, do I
    need to loop through the array with strpos() or is there a quicker method ?

    There are many needles in the table so I start by looping through
    the select result.

    This is the schematic of what I am trying to do...

    PHP Code:

    $haystack 
    = array();

      while( 
    $row mysql_fetch_assoc($result)) {
       
    $needle $row['data'];

        
    $num noof times $needle is in $haystack //   $haystack is an array
     
        
    if ($num ) {
            $
    $haystack[] = $needle// hopfully this adds the needle
            
    }

         } 
    // end while. Get next needle. 
    Does anyone know how do I do this bit:

    $num = no. of times $needle is in $haystack


    thanks
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,446
    Thanks
    13
    Thanked 361 Times in 357 Posts

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    OK,
    Looked at that but it looks as if I need:
    array_values($array)

    using this - I guess I would still need to step through the array
    wouldn't I ?

    How about if I implode() my array and then do a substr_count()
    then I could do it without looping couldn't I ?

    At a guess the array may contain 1000 values at the most.

    So would this make sense:
    PHP Code:
    $haystack = array();
    $haystack implode(","$haystack);

      while( 
    $row mysql_fetch_assoc($result)) {
       
    $needle $row['data'];

        
    $num substr_count($haystack$needle);

        if (
    $num ) {
            
    $haystack $haystack.",".$needle;  // hopefully this adds the needle
            
    }

         } 
    // end while. Get next needle. 
    In fact I don't even need the array until after I have
    added all built up the string so I could do:

    PHP Code:
    $haystack '';

      while( 
    $row mysql_fetch_assoc($result)) {
       
    $needle $row['data'];

        
    $num substr_count($haystack$needle);

        if (
    $num ) {
            
    $haystack $haystack.",".$needle;  // hopefully this adds the needle
            
    }

         } 
    // end while. Get next needle.  

    $my_array explode',' $haystack); 
    That makes more sense - anything wrong with doingit this way ?
    Last edited by jeddi; 01-25-2010 at 10:58 AM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,446
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by jeddi View Post
    OK,
    Looked at that but it looks as if I need:
    array_values($array)
    maybe you didn’t read that:
    Quote Originally Posted by php.net
    search_value

    If specified, then only keys containing these values are returned.
    that is, a count of the returned array gives you the number of occurences. should be enough for a simple match, otherwise you may want to use something like array_filter().

  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    No didn't see that
    I went to this page:
    array-values

    Anyway - I guess my solution is OK ?

    After using $haystack = ltrim($haystack, ',');
    to clean it up I exploded it into an array

    seems to work fine.

    BTW - You couldn't help me out by looking at my other
    problem in the thread titled:
    Why is my script writing 500,000 records ?

    Thanks for all your help.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.


  •  

    Posting Permissions

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