SitePoint Sponsor

User Tag List

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

    how to compare elements of 2 arrays?

    I have 2 arrays :
    -------------------------------------
    $domains = array("strawberries.com","bananas.com","apples.com");

    // add selected item to array2
    if ($add) { $selected_domains[]=$add; }
    -------------------------------------
    How can I iterate through array1 and compare each element in array1 to each element in array2 without messing up the list order that is being displayed?

    What I have working so far is comparing array1 to the last added element of array2, but I would like to compare array1 to each element of array2 meaning that if a domain has been added that the "delete" link should be displayed and if the domain has not been added the "add" link should be displayed.

    I tried several things but I can't get it to work properly.

    --------------------------------------
    <?php

    session_start();

    // domain variables

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


    // add selected item to array

    if ($add) { $selected_domains[]=$add;

    }

    // delete selected item from array

    if ($delete) {

    $d=($i-1);

    array_splice($selected_domains,$d,1);
    }


    // register selected_domains

    session_register("selected_domains");

    $i=0;

    foreach ($domains as $domainlist) {

    $i++;

    if ($domainlist==$add) { echo"<A HREF=$PHP_SELF?delete=$i>Delete $domainlist from list</A><br>";}

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

    }
    ---------------------------------------
    Last edited by jamesbond; Jun 10, 2001 at 07:04.

  2. #2
    SitePoint Enthusiast drisley's Avatar
    Join Date
    Jun 2001
    Location
    Florida
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out the in_array() function. You can use that to test if $add is in the array, and if it is go to the delete function, if it isn't add it. I think that'll be easier, if I'm understanding what you have in mind correctly.
    David Risley
    Web Developer, Web Publisher, Blogger, Tech Dude
    PC Mechanic | WebbyOnline

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

    Wrong datatype for first argument in call to in_array() in ..

    Thanks!

    in_array is exactly what I need, however I keep getting the following error :

    Wrong datatype for first argument in call to in_array() in ..

    Below is a little example, I just can't figure out what is wrong with it...

    <?

    // Define arrays

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

    foreach ($domains as $domainlist) {


    if (in_array ($selected_domains, $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>";
    }

    }

    ?>

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    rtfm at:

    http://www.php.net/manual/en/function.in-array.php

    and you will see that this is the format:

    in_array ($domainlist, $selected_domains)
    Last edited by 7stud; Jun 11, 2001 at 11:15.


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
  •