Zend - The PHP Company




Arrays

Add Code


array_merge_assoc Function  

Type: application
Added by: euromarkus
Entered: 09/12/2005
Last modified: 02/12/2007
Rating: - (fewer than 3 votes)
Views: 2905
Works on a like idea as array_merge(), but instead of concatenating arrays, it takes two related indexed arrays and creates one associative array.


<?php
/*
    Name:         array_merge_assoc
    Author:     Markus Diersbock
    Details:     Takes two related indexed arrays and
            combines them into one associative array.
            
    Notes:        If $keyarray is NOT greater or equal to $valuearray
            the function will error unless the optional $force=TRUE.

    Revisions:    2003/12/13 - Created
            2004/01/14 - Added code for unequal arrays
*/


function array_merge_assoc($keyarray$valuearray$force=FALSE){
    
$i=0;

    if ((
count($keyarray) >= count($valuearray)) || $force==TRUE) {
        foreach(
$keyarray as $element){
            
$aryreturn[$element]=$valuearray[$i++];
        }
    } else {
        return 
'Arrays are not of equal size';
    }

    return 
$aryreturn;
}

?>


Usage Example




Rate This Script





Search



This Category All Categories