Zend - The PHP Company




Arrays

Add Code


array expand  

Type: code fragment
Added by: robf
Entered: 28/05/2001
Last modified: 06/12/2000
Rating: - (fewer than 3 votes)
Views: 5231
an enhanced version of array_merge - only adds on unique elements. Only works for simple (i.e. not associative) arrays


<?php
function array_expand($base 0$extra 0)  {
 
    
// Take two one-dimensional array, step through the first looking for
    // each element of the second. If any of the elements in the second
    // array are not in the first, append them. Kind of an extended version
    // of PHP4's array_merge() function
 
    
if (!is_array($base))
        return 
false;
 
    
$ext_arr = (is_array($extra)) ? $extra : array($extra);
 
    for (
$i 0$i sizeof($ext_arr); $i++) {
 
        if (!
in_array($ext_arr[$i], $base))
            
$base[] = $ext_arr[$i];
 
    }
 
    return 
$base;
 
}
?>


Usage Example


$new_array = $array1 + $array2;


Rate This Script





Search



This Category All Categories