Advanced Search

Results 1 to 8 of 8

Thread: Looking for values in an array

  1. #1
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default Looking for values in an array

    Is there a better way to do this:

    PHP Code:
    $notAllowed = array('content-type:''mime-version''multipart/mixed''Content-Transfer-Encoding''to:''cc:''bcc:');

        foreach ( 
    $notAllowed as $na) {

            foreach ( 
    $_POST as $post ) {
                if ( 
    strpos($post$na) !== false ) {
                    
    $invalid true;
                    break;
                }
            }
        } 
    Maybe something like array_search() ?

    Thanks in advance!

    J

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I thought of in_array, but it doesn't work.

    PHP Code:
    $array = array('test''php''etc');

    $string 'I like php';

    if ( 
    in_array($string$array) )
       echo 
    'In array';
    else
        echo 
    'Not in array.'
    The above won't find the word 'php' in $string.

    Maybe a regex?

    I was just trying to avoid nesting the foreach loops.

  4. #4
    Join Date
    Feb 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    in_array checks for the hole thing (like it should)

    $A = "this is what im looking for";
    $B = array("this","is","what","im,"looking","for");
    in_array($A,$b)

    if you want to check for parts you could break the string into parts and then check thous.
    somthing like this
    PHP Code:
    function string_in_array($string="",$combobreaker=" ",$arraylist=array())
    {
    $parts explode($combobreaker$string);
    $found false;
    foreach(
    $parts as $part)
    {
         if(
    in_array($part,$arraylist) != false)
         {
              
    $found true;
              break;
         }   
    }
    return 
    $found;

    returns true if found or false if not.

    example
    PHP Code:
    <?php
    if(string_in_array("I like php"," ",array('test''php''etc')))
    {
         echo 
    "found it!";
    }
    ?>
    would output "found it!"

    you could also do it this way
    PHP Code:
    function array_in_string($stringtest="",$arraylist=array())
    {
         
    $found false;
         foreach(
    $arraylist as $item)
         {
                if(
    strpos($stringtest,$item) != false)
                {
                       
    $found true;
                       break;
                }
          }
          return 
    $found;

    I did not check these but both look like they should work
    not sure what way is faster thou.
    Last edited by zeromadpeter; 02-09-2009 at 03:58 PM. Reason: added example. added other way.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    function array_in_string($stringtest="",$arraylist=array())
    {
         $found = false;
         foreach($arraylist as $item)
         {
                if(strpos($stringtest,$item) != false)
                {
                       $found = true;
                       break;
                }
          }
          return $found;
    }
    I think this is kind of backwards from what was required... it's also hugely redundant. Don't set defaults for necessary parameters, and since we're returning immediately when we find a value anyway, there's no point in the separate variable to return.
    Code:
    // Doesn't really matter which order these parameters are in.
    // The PHP folks don't seem to care much for consistency.
    function substr_in_array($needle, $haystack) {
        foreach ($haystack as $val)
            if (strpos($val, $needle) !== false)
                return true;
    
        return false;
    }
    It would be nice to do it Haskell-style, any (needle `isInfixOf`) haystack but I can't seem to find any way to lazily terminate array_filter(). We could do it manually, I guess:
    Code:
    function id($a) {
        return $a;
    }
    
    function array_any($haystack, $predicate = 'id') {
        foreach ($haystack as $val)
            if ($predicate($val))
                return true;
    
        return false;
    }
    
    function substr_in_array($needle, $haystack) {
        $pred = create_function('$v',
                                sprintf('return strpos($v, \'%s\') !== false;',
                                        addslashes($needle)));
    
        return array_any($haystack, $pred);
    }
    It's... rather a bit clumsier, but what can one expect from PHP?
    Last edited by Twey; 02-10-2009 at 06:56 AM. Reason: Fix broken default.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. The Following User Says Thank You to Twey For This Useful Post:

    JasonDFR (02-10-2009)

  7. #6
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    PHP Code:
    function substr_in_array($needle$haystack) {
        foreach (
    $haystack as $val)
            if (
    strpos($val$needle) !== false)
                return 
    true;

        return 
    false;

    I like this Twey, but what if you have 10 needles to check for, or better, an array of 10 needles?

    In this case would you put the substr_in_array() function inside a foreach loop of the needles array? Check out my first post. Thanks.

  8. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    I think one of these functions, or one of their neighbors on php.net should be helpful here, depending on how you apply the logic:
    http://www.php.net/manual/en/function.array-map.php
    http://www.php.net/manual/en/function.array-walk.php

    For example:
    PHP Code:
    $x 0;
    function 
    in_array2($item,$string) {
    global 
    $string;
    if (
    strpos($string,$item)!==FALSE) {
    global 
    $x;
    $x 1;
    }
    array_walk('in_array2',$array);
    echo 
    $x
    Now that I look at it, there are several odd issues with this, thoughI think that may work.
    Anyway, the functions seem useful.

    This may already be solved, though.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Well, it should theoretically be easy enough to modify it (the second version) to do that. However, in PHP we end up with something like:
    Code:
    function substr_in_array($needle, $haystack) {
        if (!is_array($needle))
            $needle = array($needle);
     
        $haystack = addslashes(serialize($haystack));
     
        return array_any($needle, create_function('$a',
            "return array_any(unserialize('$haystack'), create_function('\$b',
                \"return strpos(\\\$b, \$a) !== false;\"
            ));"
        ));
    }
    ... so I think we're just going to stick with for loops...
    Code:
    function substr_in_array($needle, $haystack) {
        if (!is_array($needle))
            $needle = array($needle);
    
        foreach ($needle as $a)
            foreach ($haystack as $b)
                if (strpos($b, $a) !== false)
                    return true;
    
        return false;
    }
    The nice folks in ##php assure me that soon we'll be able to do:
    Code:
    function substr_in_array($needle, $haystack) {
      if (!is_array($needle))
        $needle = array($needle);
     
      return array_any($needle, function($a) using ($haystack) {
        return array_any($haystack, function($b) using ($a) {
          return strpos($b, $a) !== false;
        });
      });
    }
    This is still a far cry from the Haskell substr_in_array needle haystack = any (any (`isInfixOf` haystack)) needle but beggars can't be choosers, eh?
    Last edited by Twey; 02-10-2009 at 02:19 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •