SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    North Richmond
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Assistance with arrays please...

    G'day to all,

    I don't know if it is a case of me just being tired but I seem to be missing something with the array() function.

    Question: Is there a way to place and array within an array.

    E.g.
    PHP Code:
    // I have a list that user 1 can use
    $user1 = array("Dog""Cat""Fish");

    // now user 2 has all the same list but additional animals
    $user2 = array("Dog""Cat""Fish""Lion""Bear");

    // and user 3 has more animals
    $user3 = array("Dog""Cat""Fish""Lion""Bear""Tiger""Elephant"); 
    Is there a way that I can place user1's array into user2 and so forth?
    Regards,
    BJ Duncan

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a list of functions you can use in PHP: http://www.php.net/array
    You could try array_merge and array_unique if you don't want duplicates.
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    ::==:: Bonzo_CS's Avatar
    Join Date
    Dec 2003
    Location
    (Cardiff Wales)
    Posts
    747
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All you need to do is make the array multidimensional:

    // I have a list that user 1 can use
    $arr1 = array("Dog", "Cat", "Fish");

    // now user 2 has all the same list but additional animals
    $arr2 = array("Cat", "Fish", "Lion", "Bear");

    $end = array($arr1, $arr2);

    $multi_array = array($end);
    Will output

    Array
    (
    [0] => Array
    (
    [0] => Array
    (
    [0] => Dog
    [1] => Cat
    [2] => Fish
    )

    [1] => Array
    (
    [0] => Cat
    [1] => Fish
    [2] => Lion
    [3] => Bear
    )

    )

    )

    and so on....

    Or if you want to do it by merging the results use what hexburner suggests : array_merge

  4. #4
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    North Richmond
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys for your suggestions.

    The multi-dimensional arrays is not what I was looking for, sorry Bonzo.

    Hexburner, thanks for your guidence, I have utilised one of the methods from the php.net functions page and it works like a charm.
    Regards,
    BJ Duncan


Tags for this Thread

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
  •