SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How do I check if received data match one of the values in an array

    Hello,
    I receive a data with the method:
    Code:
    $data = $_POST['data'];
    I want to check if the data received match one of these values in an array:
    Code:
    $array = array(
    	"foo" => "foo",
    	"bar" => "bar",
    );
    I want the result to be like this:
    Code:
    if ( $data == $array ) {
    	// do something
    } else {
    	// return
    }
    How do I achieve this and thanks in advance.

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,014
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    The function that you're looking for is array_search()
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thank you

  4. #4
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @ketting00 ;
    Try,

    in_array()

    also...
    Quality codes are optimized and tested...
    Click here for inspiration..

  5. #5
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thank you solidcodes

    I think in_array() better suits my need. It's the first time that I learn that PHP also case-sensitive.


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
  •