AUTO
[ID/PWã±â] [ȸ¿ø°¡ÀÔ]
´º½º¿Í Á¤º¸ (276)
ÀÚÀ¯ °Ô½ÃÆÇ (392)
°¡ÀÔ Àλç (1960)
°øÁö»çÇ× (5)
AJAX ÇнÀ (72)
ASP Áú¹® (71)
ASP ÇнÀ (271)
MYSQL ÇнÀ (73)
SQLSERVER ÇнÀ (337)
¿À¶óŬ ÇнÀ (66)
µ¥ÀÌŸº£À̽º ÇнÀ (29)
¾×¼Ç½ºÅ©¸³Æ® ÇнÀ (261)
¾×¼Ç½ºÅ©¸³Æ® Áú¹® (65)
ÆÛ¿Â Ç÷¡½Ã (204)
Ç÷¡½Ã ³×ºñ°ÔÀÌ¼Ç (59)
Ç÷¡½Ã ¼Ò½º (532)
Ç÷¡½Ã ¿¬±¸½Ç (536)
Ç÷¡½Ã Áú¹® (647)
Ç÷¡½Ã °ÔÀÓ (53)
Ç÷¡½Ã ÀÎÆ®·Î (44)
Ç÷¡½Ã ÀÌÆåÆ® (45)
ÀÚÀÛ Ç÷¡½Ã (83)
ű×,HTML Áú¹® (43)
ű×,HTML ÇнÀ (131)
ÀÚ¹Ù,JSP ÇнÀ (161)
ÀÚ¹Ù,JSP Áú¹® (70)
ÀÚ¹Ù½ºÅ©¸³Æ® ÇнÀ (223)
ÀÚ¹Ù½ºÅ©¸³Æ® ¼Ò½º (73)
ÀÚ¹Ù½ºÅ©¸³Æ® Áú¹® (79)
Æ÷Åä¼¥ Áú¹® (108)
Æ÷Åä¼¥ ÇнÀ (369)
Ãßõ À̹ÌÁö (239)
ÀÚÀÛ À̹ÌÁö (81)
ÆÛ¿Â À̹ÌÁö (445)
PHP Áú¹® (62)
PHP ÇÔ¼ö (2658)
PHP ÇнÀ (661)
ÇÁ·Î±×·¡¹Ö ÇнÀ (205)
ÀÎÅͳݼ­¹ö ÇнÀ (346)
ÀÎÅͳݼ­¹ö Áú¹® (22)
³×À̹ö ¼Ó ȨÇǵðÀÚÀδåÄÄ
´ÙÀ½ ¼Ó ȨÇǵðÀÚÀδåÄÄ
±¸±Û ¼Ó ȨÇǵðÀÚÀδåÄÄ
¿¥ÆÄ½º ¼Ó ȨÇǵðÀÚÀδåÄÄ
¾ßÈÄ ¼Ó ȨÇǵðÀÚÀδåÄÄ
³×ÀÌÆ® ¼Ó ȨÇǵðÀÚÀδåÄÄ
½ÎÀÌ¿ùµå ȨÇǵðÀÚÀδåÄÄ
¿¥¿¡½º¿£ ȨÇǵðÀÚÀδåÄÄ
  ȨÇǵðÀÚÀδåÄÄ > PHP ÇÔ¼ö
   
  PHP array_merge_recursive
  ±Û¾´ÀÌ : ¼Õ´Ô     ³¯Â¥ : 05-12-20 14:48     Á¶È¸ : 1641    
   http://cafe.naver.com/q69/9298 (327)

array_merge_recursive

(PHP 4 >= 4.0.1, PHP 5)

array_merge_recursive -- µÎ°³ ÀÌ»óÀÇ ¹è¿­À» Àç±ÍÀûÀ¸·Î º´ÇÕÇÑ´Ù

¼³¸í

array array_merge_recursive ( array array1, array array2 [, array ...])

array_merge_recursive()´Â µÎ°³ ÀÌ»óÀÇ ¹è¿­ ¿ä¼ÒµéÀ» º´ÇÕÇÏ¿© ÀÌÀü ¹è¿­ÀÇ ³¡¿¡ ´Ù¸¥ ¹è¿­°ªµéÀ» Ãß°¡Çϰí ÀÌ ¹è¿­À» ¹ÝȯÇÑ´Ù.

ÀԷµǴ ¹è¿­¿¡ °°Àº ¹®ÀÚ¿­ ۰¡ ÀÖÀ¸¸é, ÀÌ Å°¿¡ ´ëÇÑ °ªÀ» ¹è¿­¿¡ °°ÀÌ º´ÇÕ½Ã۰í, ±×¸®°í ¹è¿­À» °ªÀ¸·Î °®´Â °æ¿ì ´Ù¸¥ ¹è¿­ ¾ÈÀÇ °°Àº À̸§ÀÇ ¹è¿­¿¡ ±× °ªµéÀ» º´ÇÕÇÒ ¼ö ÀÖµµ·Ï ÀÌ µ¿ÀÛÀ» Àç±ÍÀûÀ¸·Î ¼öÇàÇØ ³ª°£´Ù. ÇÏÁö¸¸, ¹è¿­ÀÌ °°Àº ¼ýÀÚ Å°¸¦ °®´Â´Ù¸é, µÚ¿¡ ¿À´Â °ªÀº ¿ø·¡ °ªÀ» µ¤¾î¾²Áö ¾Ê°í Ãß°¡µÉ°ÍÀÌ´Ù.

¿¹ 1. array_merge_recursive() ¿¹Á¦ÄÚµå

<?php
$ar1
= array ("color" => array ("favorite" => "red"), 5
);
$ar2 = array (10, "color" => array ("favorite" => "green", "blue"
));
$result = array_merge_recursive ($ar1, $ar2
);
?>

The $result will be:

Array
(
   [color] => Array
       (
           [favorite] => Array
               (
                   [0] => red
                   [1] => green
               )

           [0] => blue
       )

   [0] => 5
   [1] => 10
)

array_merge() Âü°í.



add a note add a note User Contributed Notes
array_merge_recursive
jason at ebasterpro dot com
19-Aug-2005 04:56
This modifications allows you to merge arrays of objects and objects of arrays recursively.
/**
 * arrayobj_merge_recursive2()
 *
 * Similar to array_merge_recursive2 but supports objects and arrays, keyed-valued are always overwritten.
 * Priority goes to the 2nd array. And support Object Array mixture
 *
 * @static yes
 * @public yes
 * @param $paArray1 array/object
 * @param $paArray2 array/object
 * @return array/object
 */
function arrayobj_merge_recursive2($paArray1, $paArray2)
{
   if(is_array($paArray2))
   {
       foreach ($paArray2 AS $sKey2 => $sValue2)
       {
           $paArray1[$sKey2] = arrayobj_merge_recursive2(@$paArray1[$sKey2], $sValue2);
       }
   }
   elseif(is_object($paArray2))
   {   
       foreach ($paArray2 AS $sKey2 => $sValue2)
       {
             $paArray1->{$sKey2} = arrayobj_merge_recursive2(@$paArray1->{$sKey2}, $sValue2);
       } 
   } else {
         return $paArray2;
   } 
   return $paArray1;
}
mikeo
05-Apr-2005 03:42
Similarly, you can cast variables to type array.  This is especially useful if you're passing arrays by reference.

function addHeader(&$lArray, $descript-xion = null)
{
   ...

   $lArray = array_merge_recursive((array)$lArray, $tmpArray);

   ...
}
fire at firepages dot com dot au
21-Dec-2004 03:10
PHP5 note , in PHP4 you could pass an uninitialised array to array_merge_recursive which would issue a notice but not break anything ..

while( $whatever){
 $uninitialised_array = array_merge_recursive( $uninitialised_array, $t ) ;
}

in PHP5 , if you dont initialise the array the recursion never starts so $uninitialised_array = array(); solves (good practive anyway I suppose?)
manicdepressive at mindless dot com
22-Jun-2004 02:30
Please be aware that under circumstances where you have
both the key and value common between the two arrays at a given node,
array_merge_recursive() will behave differently if that value is NULL,
as opposed to a non-null value.

i.e., I expected the results of the first two sections below to
have the same structure, but they don't. 
If this might apply to you, please see for yourself.

<pre>
<?php

$a1
= array('a'=>'b'
);
$a2 = array('a'=>'b'
);
$a3 = array_merge_recursive($a1,$a2
);
var_export($a3
);
echo
"\n\n"
;

$a1 = array('a'=>NULL
);
$a2 = array('a'=>NULL
);
$a3 = array_merge_recursive($a1,$a2
);
var_export($a3
);
echo
"\n\n"
;

$a1 = array('a'=>'b'
);
$a2 = array('a'=>NULL
);
$a3 = array_merge_recursive($a1,$a2
);
var_export($a3
);
echo
"\n\n"
;

?></pre>

This behavior also occurs if the value is the empty array.
In fact, in the above example, interchanging the empty array with
any and all occurences of NULL will yield the same result.

   code till dawn!  -mark
brian at vermonster dot com
25-May-2004 09:44
Here is a fairly simple function that replaces while recursing.

<?php
/**
 * array_merge_recursive2()
 *
 * Similar to array_merge_recursive but keyed-valued are always overwritten.
 * Priority goes to the 2nd array.
 *
 * @static yes
 * @public yes
 * @param $paArray1 array
 * @param $paArray2 array
 * @return array
 */
function array_merge_recursive2($paArray1, $paArray2
)
{
   if (!
is_array($paArray1) or !is_array($paArray2)) { return $paArray2
; }
   foreach (
$paArray2 AS $sKey2 => $sValue2
)
   {
      
$paArray1[$sKey2] = array_merge_recursive2(@$paArray1[$sKey2], $sValue2
);
   }
   return
$paArray1
;
}

?>

Examples:
<?php

$array1
= array(
  
'liquids'
=> array(
      
'water' => array('cold', 'fizzy', 'clean'
)
       ,
'beer' =>
'warm'
  
)
);

$array2
= array(
  
'liquids'
=> array(
      
'water' =>
'hot'
      
,'milk' =>
'wet'
  
)
);

$result1 = array_merge_recursive2($array1, $array2
);
$result2 = array_merge_recursive2($array2, $array1
);
?>

Result 1 is:
Array
(
   [liquids] => Array
       (
           [water] => hot
           [beer] => warm
           [milk] => wet
       )
)

Result 2 is:
Array
(
   [liquids] => Array
       (
           [water] => Array
               (
                   [0] => cold
                   [1] => fizzy
                   [2] => clean
               )

           [milk] => wet
           [beer] => warm
       )
)
paska at kios dot sk
04-Mar-2004 10:18
This emulates replace of $_REQUEST according to variable_order=GPC.
<?
  
function array_merge_replace($array, $newValues
) {
       foreach (
$newValues as $key => $value
) {
           if (
is_array($value
)) {
               if (!isset(
$array[$key
])) {
                  
$array[$key
] = array();
               }
              
$array[$key] = array_merge_replace($array[$key], $value
);
           } else {
              
$array[$key] = $value
;
           }
       }
       return
$array
;
   }

  
$_REQUEST = array_merge_replace($_REQUEST, $_GET
);
  
$_REQUEST = array_merge_replace($_REQUEST, $_POST
);
  
$_REQUEST = array_merge_replace($_REQUEST, $_COOKIE
);
?>

Useful with stripping backslashes at beginning of main include file:
<?
if (get_magic_quotes_gpc() == 1
) {

   function
stripMagicSlashes($element
) {
       if (
is_array($element
)) {
           return
array_map("stripMagicSlashes", $element
);
       } else {
           return
stripslashes($element
);
       }
   }

   function
array_merge_replace($array, $newValues
) {
       foreach (
$newValues as $key => $value
) {
           if (
is_array($value
)) {
               if (!isset(
$array[$key
])) {
                  
$array[$key
] = array();
               }
              
$array[$key] = array_merge_replace($array[$key], $value
);
           } else {
              
$array[$key] = $value
;
           }
       }
       return
$array
;
   }

  
$_GET    = array_map("stripMagicSlashes", $_GET
);
  
$_POST  = array_map("stripMagicSlashes", $_POST
);
  
$_COOKIE = array_map("stripMagicSlashes", $_COOKIE
);

  
$_REQUEST = array_merge_replace($_REQUEST, $_GET
);
  
$_REQUEST = array_merge_replace($_REQUEST, $_POST
);
  
$_REQUEST = array_merge_replace($_REQUEST, $_COOKIE
);

}

$GLOBALS['stripped'] = true
;
?>

Based on examples from users from this site.
t dot tom at succont dot de
09-Jan-2004 04:09
Here my modification of shemari's Code for Replacing Values in an Array. My modification will return the new Array, not handle it by reference.
Original Array will not be touched.

Hope it helps anyone. Most thanks goes to shemari ;o)

<?php
  
/**
     * Merges two arrays and replace existing Entrys
     *
     * Merges two Array like the PHP Function array_merge_recursive.
     * The main difference is that existing Keys will be replaced with new Values,
     * not combined in a new Sub Array.
     *
     * Usage:
     *        $newArray = array_merge_replace( $array, $newValues );
     *
     * @access puplic
     * @author Tobias Tom <t.tom@succont.de>
     * @param array $array First Array with 'replaceable' Values
     * @param array $newValues Array which will be merged into first one
     * @return array Resulting Array from replacing Process
     */
function array_merge_replace( $array, $newValues
) {
   foreach (
$newValues as $key => $value
) {
       if (
is_array( $value
) ) {
               if ( !isset(
$array[ $key
] ) ) {
              
$array[ $key
] = array();
           }
          
$array[ $key ] = $this->array_merge_replace( $array[ $key ], $value
);
       } else {
           if ( isset(
$array[ $key ] ) && is_array( $array[ $key
] ) ) {
              
$array[ $key ][ 0 ] = $value
;
           } else {
               if ( isset(
$array ) && !is_array( $array
) ) {
                  
$temp = $array
;
                  
$array
= array();
                  
$array[0] = $temp
;
               }
              
$array[ $key ] = $value
;
           }
       }
   }
   return
$array
;
}
?>
shemari75 at mixmail dot com
19-Dec-2003 05:22
Here's a function to recursively merge any number of any-dimensional arrays.
It actually works in quite a similar way as array_merge_recursive does, but with two major differences:
- Later elements overwrite previous ones having the same keys.
- Numeric keys are not appended. Instead, they are converted into associative ones, and therefore overwritten as stated above.

Usage:
   array array_merge_n(array array1, array array2[, array ...])

<?php
  
/**
     *  Merges two arrays of any dimension
     *
     *  This is the process' core!
     *  Here each array is merged with the current resulting one
     *
     *  @access private
     *  @author Chema Barcala Calveiro <shemari75@mixmail.com>
     *  @param array $array  Resulting array - passed by reference
     *  @param array $array_i Array to be merged - passed by reference
     */

  
function array_merge_2(&$array, &$array_i
) {
      
// For each element of the array (key => value):
      
foreach ($array_i as $k => $v
) {
          
// If the value itself is an array, the process repeats recursively:
          
if (is_array($v
)) {
               if (!isset(
$array[$k
])) {
                  
$array[$k
] = array();
               }
              
array_merge_2($array[$k], $v
);

          
// Else, the value is assigned to the current element of the resulting array:
          
} else {
               if (isset(
$array[$k]) && is_array($array[$k
])) {
                  
$array[$k][0] = $v
;
               } else {
                   if (isset(
$array) && !is_array($array
)) {
                      
$temp = $array
;
                      
$array
= array();
                      
$array[0] = $temp
;
                   }
                  
$array[$k] = $v
;
               }
           }
       }
   }

  
/**
     *  Merges any number of arrays of any dimension
     *
     *  The arrays to be merged are passed as arguments to the function,
     *  which uses an external function (array_merge_2) to merge each of them
     *  with the resulting one as it's being constructed
     *
     *  @access public
     *  @author Chema Barcala Calveiro <shemari75@mixmail.com>
     *  @return array Resulting array, once all have been merged
     */

  
function array_merge_n
() {
      
// Initialization of the resulting array:
      
$array
= array();

      
// Arrays to be merged (function's arguments):
      
$arrays =& func_get_args
();

      
// Merging of each array with the resulting one:
      
foreach ($arrays as $array_i
) {
           if (
is_array($array_i
)) {
              
array_merge_2($array, $array_i
);
           }
       }

       return
$array
;
   }
?>

   

 
´º½º¿Í Á¤º¸ ¡¤ ÀÚÀ¯ °Ô½ÃÆÇ ¡¤ °¡ÀÔ ÀÎ»ç ¡¤ °øÁö»çÇ× ¡¤ AJAX ÇнÀ ¡¤ ASP Áú¹® ¡¤ ASP ÇнÀ ¡¤ MYSQL ÇнÀ ¡¤ SQLSERVER ÇнÀ ¡¤ ¿À¶óŬ ÇнÀ ¡¤ µ¥ÀÌŸº£À̽º ÇнÀ ¡¤ ¾×¼Ç½ºÅ©¸³Æ® ÇнÀ ¡¤ ¾×¼Ç½ºÅ©¸³Æ® Áú¹® ¡¤ ÆÛ¿Â Ç÷¡½Ã ¡¤ Ç÷¡½Ã ³×ºñ°ÔÀÌ¼Ç ¡¤ Ç÷¡½Ã ¼Ò½º ¡¤ Ç÷¡½Ã ¿¬±¸½Ç ¡¤ Ç÷¡½Ã Áú¹® ¡¤ Ç÷¡½Ã °ÔÀÓ ¡¤ Ç÷¡½Ã ÀÎÆ®·Î ¡¤ Ç÷¡½Ã ÀÌÆåÆ® ¡¤ ÀÚÀÛ Ç÷¡½Ã ¡¤ ű×,HTML Áú¹® ¡¤ ű×,HTML ÇнÀ ¡¤ ÀÚ¹Ù,JSP ÇнÀ ¡¤ ÀÚ¹Ù,JSP Áú¹® ¡¤ ÀÚ¹Ù½ºÅ©¸³Æ® ÇнÀ ¡¤ ÀÚ¹Ù½ºÅ©¸³Æ® ¼Ò½º ¡¤ ÀÚ¹Ù½ºÅ©¸³Æ® Áú¹® ¡¤ Æ÷Åä¼¥ Áú¹® ¡¤ Æ÷Åä¼¥ ÇнÀ ¡¤ Ãßõ À̹ÌÁö ¡¤ ÀÚÀÛ À̹ÌÁö ¡¤ ÆÛ¿Â À̹ÌÁö ¡¤ PHP Áú¹® ¡¤ PHP ÇÔ¼ö ¡¤ PHP ÇнÀ ¡¤ ÇÁ·Î±×·¡¹Ö ÇнÀ ¡¤ ÀÎÅͳݼ­¹ö ÇнÀ ¡¤ ÀÎÅͳݼ­¹ö Áú¹®¡¤ Ç÷¡½Ã »ùÇà ¼Ò½º ¡¤ Ç÷¡½Ã äÆÃ¹æ ¡¤ Ç÷¡½Ã ¹Ì´ÏȨÇÇ ¡¤ Ç÷¡½Ã °¶·¯¸® ¡¤ Ç÷¡½Ã RSS¸®´õ ¡¤ Ç÷¡½Ã PHP »çÀü ¡¤ Ç÷¡½Ã ¿ìÆí¹øÈ£ ã±â ¡¤ Ç÷¡½Ã ÇÑÀÚ°ÔÀÓ ¡¤ Ç÷¡½Ã ´Þ·Â°ú½Ã°è ¡¤ Ç÷¡½Ã Æ÷Åä¶óÀ̺귯¸® ¡¤ ·£´ýÀ̹ÌÁö ¡¤ Ç÷¡½Ã ´Þ·Â°ú½Ã°è ¡¤ ÇÑ±Û µµ¸ÞÀÎ ¡¤ ³×À̹ö ºí·Î±× ½ºÅ©·¦ ¡¤ ½ÎÀÌ¿ùµå ¹Ì´ÏȨÇÇ °¶·¯¸® ¡¤ ½ÎÀÌ¿ùµå ¹Ì´ÏȨÇÇ µî·Ï ¡¤ ÇÁ·Î°ÔÀÌ¸Ó ·©Å© ¡¤ ȨÆäÀÌÁöÁ¤º¸ ºí·Î±×
Copyright ¨Ï hompy.info. All rights reserved.