예제 #1
0
/**
 * An array-merging function to strip one or more arrays down to a single one dimension array
 */
function array_merge_deep($arr)
{
    $arr = (array) $arr;
    $argc = func_num_args();
    if ($argc != 1) {
        $argv = func_get_args();
        for ($i = 1; $i < $argc; $i++) {
            $arr = array_merge($arr, (array) $argv[$i]);
        }
    }
    $temparr = array();
    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            $temparr = array_merge($temparr, array_merge_deep($value));
        } else {
            $temparr = array_merge($temparr, array($key => $value));
        }
    }
    return $temparr;
}
예제 #2
0
 function array_merge_deep($arrays)
 {
     $result = array();
     foreach ($arrays as $array) {
         foreach ($array as $key => $value) {
             // Renumber integer keys as array_merge_recursive() does. Note that PHP
             // automatically converts array keys that are integer strings (e.g., '1')
             // to integers.
             if (is_integer($key)) {
                 $result[] = $value;
             } elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
                 $result[$key] = array_merge_deep(array($result[$key], $value));
             } elseif (isset($result[$key]) && is_string($result[$key])) {
                 $result[$key] = isset($result[$key]) ? $result[$key] . ' ' . $value : $value;
             } else {
                 $result[$key] = $value;
             }
         }
     }
     return $result;
 }
예제 #3
0
 /**
  * 递归地合并一个或多个数组(不同于 array_merge_recursive )
  *
  * @return array
  */
 function array_merge_deep()
 {
     $a = func_get_args();
     for ($i = 1; $i < count($a); $i++) {
         foreach ($a[$i] as $k => $v) {
             if (isset($a[0][$k])) {
                 if (is_array($v)) {
                     if (is_array($a[0][$k])) {
                         $a[0][$k] = array_merge_deep($a[0][$k], $v);
                     } else {
                         $v[] = $a[0][$k];
                         $a[0][$k] = $v;
                     }
                 } else {
                     $a[0][$k] = is_array($a[0][$k]) ? array_merge($a[0][$k], array($v)) : $v;
                 }
             } else {
                 $a[0][$k] = $v;
             }
         }
     }
     return $a[0];
 }
/**
 * Merges 2 arrays recursively checking for nested arrays and merging them as well
 * Can either preserve numeric indexes or reorder them depending on the value
 * of $keep_numeric_index, which defaults to true
 * @param array $array1 The first array to merge
 * @param array $array2 The second array to merge
 * @param boolean $keep_numeric_index $enclosure One character long string that determines what to ues for enclosing strings
 * @return array The recursively merged arrays
 */
function array_merge_deep($array1, $array2, $keep_numeric_index = true)
{
    // the first array is in the output set in every case
    $ret = $array1;
    // merege $ret with the remaining arrays
    foreach ($array2 as $key => $value) {
        if (!$keep_numeric_index && (string) $key === (string) intval($key)) {
            // integer or string as integer key - append
            $ret[] = $value;
        } else {
            // string key - megre
            if (is_array($value) && isset($ret[$key])) {
                // if $ret[$key] is not an array you try to merge an scalar value with an array - the result is not defined (incompatible arrays)
                // in this case the call will trigger an E_USER_WARNING and the $ret[$key] will be null.
                $ret[$key] = array_merge_deep($ret[$key], $value);
            } else {
                $ret[$key] = $value;
            }
        }
    }
    return $ret;
}