SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict Iceman90's Avatar
    Join Date
    Mar 2006
    Location
    Calgary, Alberta, Canada
    Posts
    392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    in_array problem

    I am having a bit of a problem with the in_array function that is driving me nuts. I hope someone can help me with it.

    Here is my code:

    Code PHP:
    $haystack = array("red","green","orange");
     
    		function checkIt($needle){
    			if(in_array($needle,$haystack)){
    				echo "<p>Found</p>";
    			}
    		}				
    		checkIt("red");

    and I am getting the following error

    Code:
    Warning: in_array() [function.in-array]: Wrong datatype for second argument in check.php on line 3
    Can anyone tell me what I am doing wrong?

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're trying to access a global array in a function. You need to put global $haystack at the beginning of your function to access it.

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    or...
    PHP Code:
    function checkit ($needle$haystack) {...}

    checkit('red'$haystack); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Addict Iceman90's Avatar
    Join Date
    Mar 2006
    Location
    Calgary, Alberta, Canada
    Posts
    392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to both of you. I've got it working now.


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
  •