PHP Array Question

Discussion in 'PHP' started by philej, Dec 3, 2005.

  1. #1
    Hi all,

    Is there a php function to merge two indexed arrays into one assoc array?

    I know I could write this function but I wanted to use the native function if it exists and I didn't see it on php.net

    It would work like:
    
    $KEY = Array('somekey'); 
    $VALUE = Array('somevalue'); 
    
    $ASSOC = make_assoc($KEY, $vALUE); 
    
    // then $ASSOC looks like
    Array('somekey' => 'somevalue); 
    
    Code (markup):
    -phil
     
    philej, Dec 3, 2005 IP
  2. HN Will

    HN Will Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    sorry I think you'll have to write the function - only a few lines of code though.
     
    HN Will, Dec 3, 2005 IP
  3. philej

    philej Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah, I guess it's really simple. It just seemed like Zend would have allready done it for me. :) Thanks for the input.
     
    philej, Dec 3, 2005 IP
  4. HN Will

    HN Will Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No problem - yeah i've thought the same way for little functions that i was SURE someone else had written and incorporated into php. I've spent too much of my life looking for these functions when I could have written them myself in about 30 seconds.
     
    HN Will, Dec 3, 2005 IP
  5. cornelius

    cornelius Peon

    Messages:
    206
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    cornelius, Dec 3, 2005 IP
  6. philej

    philej Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This function only merges the values of an array. Not the Keys with the Values.
    -phil
     
    philej, Dec 3, 2005 IP
  7. philej

    philej Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I thought I'd post this in case anyone was curious:

    
    /**
     * Converts two indexed arrays into an associative array of KEY=>VAlUE pairs
     *
     * @param array of key names $keys
     * @param array of values to map to key names $values
     * @return associative array like $array[$key] = $value 
     */
    function make_assoc($keys, $values)
    {
        $returnAssoc = array(); 
        $valueCount = count($values); 
        for($i = 0; $i < $valueCount; $i++) 
        {
            $returnAssoc[$keys[$i]]=$values[$i];    
        } 
        return $returnAssoc; 
    }
    
    Code (markup):
     
    philej, Dec 3, 2005 IP