SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Merging Arrays

  1. #1
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Shaw AFB
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Merging Arrays

    What's the best way to merge 3 arrays together, with the more than likely possibility that one of those arrays could be empty (based on user input)
    ~ Nate L ~

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Shaw AFB
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it...but there may be an alternative, easier/better method... please do tell

    PHP Code:
    $merged = array();

    if(!empty(
    $array1)
    {
      
    $merged array_merge($merged$array1);
    }

    if(!empty(
    $array2)
    {
      
    $merged array_merge($merged$array2);
    }

    if(!empty(
    $array3)
    {
      
    $merged array_merge($merged$array3);
    }

    print_r($merged); 
    ~ Nate L ~

  4. #4
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Shaw AFB
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    Yeh - I guess I was mis-using it earlier, so I was trying array_push and a few other methods...
    ~ Nate L ~

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $merged array_merge($array1$array2$array3 ...etc...); 

  6. #6
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Shaw AFB
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    PHP Code:
    $merged array_merge($array1$array2$array3 ...etc...); 
    Does that work if the array is empty? It didn't for me.
    ~ Nate L ~

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean by if array is empty? There will not be anything if the array is empty in the merged array. Can you give us your real example?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    There may be a technical detail which is being overlooked/assumed here. By "empty" do you mean literally an empty array (array()), or the variable is set but with an empty value (NULL, ""?), will the variable not be available at all (resulting in "Undefined variable" notices)?
    Salathe
    Software Developer and PHP Manual Author.


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
  •