Top

[PHP] ¹è¿­ Áß°£¿¡ Ãß°¡Çϱâ | Web-Programing
±è°æÈÆ (admin) | Editor | 2018/01/10 10:32:50 | Á¶È¸:7840

À̹ø¿£ ¹è¿­À» ´Ù·ê¶§ Áß°£¿¡ Ãß°¡ÇÏ´Â ¹æ¹ýÀ» ´Ù·ç°Ú½À´Ï´Ù.

ÀÌ ¹æ¹ý¿¡´Â ¹è¿­°ú °ü·ÃÇÑ 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À» ÇÏ½Ã¸é µË´Ï´Ù.



Ãâó:  http://happyman73.tistory.com/61  [ÀÛÀº¾Ç¸¶`s Blog]

°øÀ¯Çϱâ
°øÀ¯Çϱâ
0
0
0


´ñ±ÛÀ» ºÒ·¯¿À´Â ÁßÀÔ´Ï´Ù.
¡ã ÀÌÀü±Û [Javascript] REPLACE¸¦ REPLACEALL ó·³ »ç¿ëÇϱ⠱è°æÈÆ (admin) 2018-07-04 11:29:33
¡å ´ÙÀ½±Û [CSS] IE ÀÎDz¹Ú½º X ¹öÆ° Á¦°ÅÇϱâ. ±è°æÈÆ (admin) 2016-05-02 11:21:11