À̹ø¿£ ¹è¿À» ´Ù·ê¶§ Áß°£¿¡ Ãß°¡ÇÏ´Â ¹æ¹ýÀ» ´Ù·ç°Ú½À´Ï´Ù.
ÀÌ ¹æ¹ý¿¡´Â ¹è¿°ú °ü·ÃÇÑ 2°³ÀÇ ÇÔ¼ö¸¦ »ç¿ëÇÕ´Ï´Ù.
°¢ ÇÔ¼ö´Â array_slice()¿Í array_merge()ÇÔ¼öÀÔ´Ï´Ù.
¾Æ·¡ Äڵ带 º¸½Ã°Ú½À´Ï´Ù.
(ÄÚµå´Â Syntax Highlighter°¡ Àû¿ëµÇ¾î ¸ð¹ÙÀÏ¿¡¼´Â ¾Èº¸ÀÏ ¼ö ÀÖ½À´Ï´Ù.)
1
2
|
$arr =
array
(
"aaa"
,
"bbb"
,
"ccc"
,
"ddd"
,
"eee"
,
"fff"
);
print_r(
$arr
);
|
À§ Äڵ带 ½ÇÇàÇÏ¸é °á°ú´Â ´ÙÀ½°ú °°½À´Ï´Ù.
"Array ( [0] => aaa [1] => bbb [2] => ccc [3] => ddd [4] => eee [5] => fff )"
ÀÌÁ¦ Äڵ带 ¼öÁ¤ÇØ º¸°Ú½À´Ï´Ù.
(ÄÚµå´Â Syntax Highlighter°¡ Àû¿ëµÇ¾î ¸ð¹ÙÀÏ¿¡¼´Â ¾Èº¸ÀÏ ¼ö ÀÖ½À´Ï´Ù.)
1
2
3
4
5
6
7
8
9
10
11
|
$arr =
array
(
"aaa"
,
"bbb"
,
"ccc"
,
"ddd"
,
"eee"
,
"fff"
);
$add =
"jjj"
;
function insert_array(
$arr
,
$idx
,
$add
){
$arr_front =
array_slice
(
$arr
, 0,
$idx
);
$arr_end =
array_slice
(
$arr
,
$idx
);
$arr_front
[] =
$add
;
return array_merge
(
$arr_front
,
$arr_end
);
}
$arr = insert_array(
$arr
, 2,
$add
);
print_r(
$arr
);
|
À§ Äڵ带 ½ÇÇàÇغ¸¸é ´ÙÀ½°ú °°Àº °á°ú°¡ ³ª¿É´Ï´Ù.
"Array ( [0] => aaa [1] => bbb [2] => jjj [3] => ccc [4] => ddd [5] => eee [6] => fff )"
insert_array()ÇÔ¼ö¸¦ È£ÃâÇÒ ¶§ »ðÀÔÇÒ idx¸¦ 2¸¦ Á¦½ÃÇ߱⠶§¹®¿¡ ÇØ´ç À妽º¿¡ Ãß°¡µÈ °ÍÀ» º¸½Ç ¼ö ÀÖ½À´Ï´Ù.
¿©±â¼ ÇÑ°¡Áö µ¡ºÙÀÌÀÚ¸é...
Áö³¹ø Æ÷½ºÆÃÇÑ "PHP¿¡¼ ¹è¿¿¡ °ªÀÌ Æ÷ÇԵǾî ÀÖ´ÂÁö ¿©ºÎ¿Í ÇØ´ç À妽º °¡Á®¿À±â" ¿Í È¥¿ëÇÏ¿©
ƯÁ¤ À妽º ´ÙÀ½¿¡ Ãß°¡¸¦ ÇÏ°íÀÚ ÇϽǶ§´Â insert_array()ÇÔ¼ö¸¦ È£ÃâÇÒ¶§ °¡Á®¿Â À妽º + 1À» ÇÏ½Ã¸é µË´Ï´Ù.