SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Warning: Wrong datatype for first argument in call to in_array() in

    Does anyone know why I get this error when I try to run the code below?

    I checked it over and over and can't find anything wrong with it...I"m guessing the foreach statement causes this error, but to my understanding the code is correct.


    Warning: Wrong datatype for first argument in call to in_array() in .... line 13

    <?

    // Define arrays

    $domains = array ("strawberries.com","bananas.com","apples.com");
    $selected = array ("strawberries.com","bananas.com");



    foreach ($domains as $domainlist) {


    if (in_array ($selected, $domainlist)) {

    echo"<A HREF=$PHP_SELF?delete=$domainlist>Delete $domainlist from list</A><br>";
    }

    else {
    echo"<A HREF=$PHP_SELF?add=$domainlist>Add $domainlist to list</A><br>";
    }

    }

    ?>

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aha, and so we meet again Mr. Bond

    Yeah, you are passing two arrays to in_array() when you want to be passing $needle (as a string or int or double, etc) and $haystack (as an array) like so:

    in_array($needle, $haystack);

    I did not know of in_array() until this moment, so I just looked it up in the manual. That's why I like hanging around here - you learn something new every day

  3. #3
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Warning: Wrong datatype for first argument in call to in_array()

    Thanks for the info freakysid,

    Do you perhaps know of an efficient way of comparing 2 'haystacks', instead of comparing a 'needle' against a 'haystack' ?


    By the way for the people who have "PHP Developer's Cookbook" , on page 85 they give an example on in_array :

    $common = array ("Marriages","were","made","in","heaven");
    $oscar_wilde = array ("Divorces","were","made","in","heaven");

    $symdiff = array();

    foreach ($common as $ele) {
    if (!in_array ($oscar_wilde, $ele)) {
    array_push ($symdiff, $ele);

    }

    }

    ...
    .....

    This gives the same error, since they use 2 arrays just like I did in my example.

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL, they just have the arguements around the wrong way. I think they mean to write this as:
    PHP Code:
    $common = array ("Marriages","were","made","in","heaven"); 
    $oscar_wilde = array ("Divorces","were","made","in","heaven"); 

    $symdiff = array(); 

    foreach (
    $common as $ele) { 
       if (!
    in_array ($ele$oscar_wilde)) { 
          
    array_push ($symdiff$ele); 
       } 

    Also, this statement:
    array_push ($symdiff, $ele);
    Is a rather extravagent way of writing:
    $sysmdiff[] = $ele;
    But each to there own

  5. #5
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    freakysid, thanks for clearing that up!


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
  •