Results 1 to 8 of 8

Thread: Combine arrays?

  1. #1
    Join Date
    Jan 2007
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Combine arrays?

    Say I have these two arrays as an example,
    PHP Code:
    $href = array('1.html''2.gif''3.php');
    $name = array('My html file''My gif file''My php file'); 
    How can I combine them to make that?:
    PHP Code:
    $combined = array("1.html" => "My html file""2.gif" => "My gif file""3.php" => "My php file"

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $combined = array();
    for($i = 0; $i < count($href); ++$i)
      $combined[$href[$i]] = $name[$i];
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    May 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    a very Nice solution to combine array...thnks buddy

  4. #4
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    actually, PHP has a built in function for this called array_combine():

    PHP Code:
    $href = array('1.html''2.gif''3.php');
    $name = array('My html file''My gif file''My php file'); 
    $combined array_combine($href,$name); 
    Link: http://www.php.net/manual/en/function.array-combine.php
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    yes, but not in the way that the original poster wanted.

    Anyway, these responses are to a thread that is already over 3 years old and solved for that matter.
    To choose the lesser of two evils is still to choose evil. My personal site

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It is an old thread, but that is a better answer. As far as I can tell it is exactly what the original poster wanted: array_combine($keys,$values);
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    I see now. I was confusing array_combine with array_merge.
    To choose the lesser of two evils is still to choose evil. My personal site

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Yes, I did too at first. I'm going to close this discussion now so it'll fall back into the archives-- and now the question is fully answered.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •