SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: array question

  1. #1
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array question

    Hello

    I have this array

    $test[]="house car book";
    $test[]="train boat random";
    $test[]="pen diary forum number boat";
    print_r($test);


    What to do to know the array keys of the array which has the word "boat" ?
    In this case the result should be [1] and [2] because these arrays contains "boat"



    Thank you
    Graziano

  2. #2
    SitePoint Zealot Dachande663's Avatar
    Join Date
    Feb 2005
    Location
    Birmingham, UK
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $keyword 'boat';
    foreach (
    $test as $key=>$value) if(strstr($keyword,$value)) $found[]=$key
    $found will now contain the array elements containing $keyword.
    Web Developer & Geek: hybridlogic.co.uk ~ lukelanchester.com

  3. #3
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you , (I was thinking there was a way to do that without using
    foreach) !

  4. #4
    SitePoint Zealot Dachande663's Avatar
    Join Date
    Feb 2005
    Location
    Birmingham, UK
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as I know there isn't a string matching function designed to run over arrays so this is probably your best bet.
    Web Developer & Geek: hybridlogic.co.uk ~ lukelanchester.com

  5. #5
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    however

    $keyword = 'boat';foreach ($test as $key=>$value) if(strstr($keyword,$value)) $found[]=$key;
    print_r($found);


    added to my code above returns nothing . Is there any error ?

  6. #6
    SitePoint Zealot Dachande663's Avatar
    Join Date
    Feb 2005
    Location
    Birmingham, UK
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My mistake, I had the arguments the wrong way round for strstr, this works.
    PHP Code:
    foreach ($test as $key=>$value) if(strstr($value,$keyword)) $found[]=$key
    Web Developer & Geek: hybridlogic.co.uk ~ lukelanchester.com

  7. #7
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Try this, it's the same code but with brackets, it might make a difference.

    PHP Code:
    $keyword 'boat';
    foreach (
    $test as $key=>$value) {
        if(
    strstr($value,$keyword)) {
            
    $found[]=$key;
        }
    }
    print_r($found); 

  8. #8
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all!

  9. #9
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Robustness is your friend. Add the ability to do case insensitive searches.

    PHP Code:
    function array_contains ($source$needle$casestrict false) {

        
    // Sanity Check
        
    if (!is_array($source)) {
            return -
    1;
        }

        
    $ret  = array();
        
    $func = ($casestrict) ? 'strpos' 'stripos'# PHP 5

        
    foreach ($source as $key => $value) {

            if (
    $func($value$neelde) !== false) {
                
    $ret[] = $key;
            }

        }

        return 
    $ret;

    }

    # PHP 4 Function

    function array_contains ($source$needle$casestrict false) {

        
    // Sanity Check
        
    if (!is_array($source)) {
            return -
    1;
        }

        if (!
    $casestrict) {
            
    $needle strtolower($needle);
        }

        
    $ret  = array();

        foreach (
    $source as $key => $value) {

            if (!
    $casestrict) {
                
    $value strtolower($value);
            }

            if (
    strpos($value$neelde) !== false) {
                
    $ret[] = $key;
            }

        }

        return 
    $ret;


    Btw use strpos to determine if a needle occurs in a sting not strstr.

    From the manual:
    If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

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