5. 배열 변수 -php-

2017. 3. 11. 04:19 from COMPUTER/php

5. 배열 변수 -php-

 

$_POST$_GET 변수도 배열 변수의 일종이다.

 

설명)

$_POST['a']에 저장된 값이 1이라면,

$_POST"배열 변수", a"", 1""이다.

배열 변수는 $변수명['키이름']으로 표기한다.

키 이름이 문자일 경우 ['a'] 또는 ["a"]로 쓰는 것이 정상.

[a]라고 써도 동작은 하지만 ['a']라고 쓰는 것이 정석이다.

 

배열 원소는 배열을 값으로 가질 수 있다.

) $a[ ], $a[ ][ ], $a[ ][ ][ ]

 

*배열의 정의:

-직접 대입법

$arr['key1']=1;

$arr['key2']=2;

$arr['key3']=3;

 

- array()문을 이용하여 키 이름 없이 입력.

$arr=array("val1", "val2", "val3");

결과 : $arr[0]=val1, $arr[1]=val2, $arr[2]=val3 -> 0부터 시작됨을 유의할 것.

 

- array()문을 이용하여 키 이름까지 정의.

$arr=array("key1"=>"val1", "key2"=>"val2", "key3"=>"val3");

 

*count(배열변수 명):

배열의 갯수를 알아낸다.

<?

$arr=array("1", "2", "3");

echo count($arr);

?>

결과 3

 

*print_r(배열변수 명):

배열변수를 출력한다.

<?

$arr=array("1", "2", "3");

print_r($arr);

?>

결과 : Array ( [0] => 1 [1] => 2 [2] => 3 )

 

*explode(구분자, 문자열);

문자열을 지정된 구분자에서 문자열로 나눈 후 배열로 리턴을 해주는 함수.

<?

$a = "손오공, 저팔계, 사오정"; // 콤마 다음에 나오는 공백은 무시된다.

$aa = explode(",", $a);

print_r($arr);

?>

결과 : Array ( [0] => 손오공 [1] => 저팔계 [2] => 사오정 )

 

*implode(구분자, 문자열);

explode와 반대 임.

<?

$arr = array("사과", "바나나", "", "");

$str = implode(":", $arr);

echo $str;

?>

결과 사과:바나나::

print_r($str)을 해도 문자 변수 출력은 echo 출력과 같다. 사과:바나나::감 

'COMPUTER > php' 카테고리의 다른 글

7. 쿠키 이용하기 -php-  (0) 2017.03.12
6. 숫자를 그림으로 대체하기 -php  (0) 2017.03.12
4. for문 -php-  (0) 2017.03.10
3. 연산자. -php-  (0) 2017.03.10
2. post 방식. -php-  (0) 2017.03.09
Posted by star story :

Snippet :: Code View :: "+location.href+'
'+b+"

");top.consoleRef.document.close()}$(".snippet-container").each(function(b){$(this).find("a.snippet-text").click(function(){var d=$(this).parents(".snippet-wrap").find(".snippet-formatted");var c=$(this).parents(".snippet-wrap").find(".snippet-textonly");d.toggle();c.toggle();if(c.is(":visible")){$(this).html("html")}else{$(this).html("text")}return false});$(this).find("a.snippet-window").click(function(){var c=$(this).parents(".snippet-wrap").find(".snippet-textonly").html();a(c);$(this).blur();return false})});$(".snippet-toggle").each(function(b){$(this).click(function(){$(this).parents(".snippet-container").find(".snippet-wrap").toggle()})})});