<?php
// PHP변수 스크립트에서 쉽게 사용하기
function setScriptVar($name, $obj) {
global $script;
$script = "<script language='javascript'>";
if(is_object($obj)) {
$script .= "var $name = new Array();";
factory($name, $obj,'object');
} elseif(is_array($obj)) {
$script .= "var $name = new Array();";
factory($name, $obj,'array');
} else {
if(is_numeric($obj)) $script .= "var $name = $obj;";
else $script .= "var $name = '$obj';";
}
$script .= "</script>";
echo $script;
}
function factory($name, $data, $type = NULL) {
global $script;
foreach($data as $key => $val) {
if(is_object($val)){
factory($name.".".$key, $val, 'object');
} elseif(is_array($val)){
if($type == 'array') {
$script .= $name."[".$key."] = new Array();";
factory($name."[".$key."]", $val, 'array');
} else {
$script .= $name.".".$key." = new Array();";
factory($name.".".$key, $val, 'array');
}
} elseif(preg_match("/[a-zA-Z_][a-zA-Z0-9_]*/",$key) && $type == 'object') {
if(is_numeric($val)) $script .= $name.".".$key."=".$val.";";
else $script .= $name.".".$key."='".$val."';";
} elseif($type == 'array') {
if(!is_numeric($key)) $key = "'".$key."'";
if(is_numeric($val)) $script .= $name."[".$key."]=".$val.";";
else $script .= $name."[".$key."]='".$val."';";
}
}
}
// 사용법 setScriptVar(변수명지정,PHP변수);
// 일반변수
$market = "I Like Fruit!";
setScriptVar("market", $market);
// 배열
$fruit = array('Apple','Pineapple',array('Tomato','Banana'));
setScriptVar("fruit", $fruit);
// 오브젝트와 배열
$list->like = 'Pineapple';
$list->hate = 'Tomato';
$list->buy = array('Apple','Pineapple');
setScriptVar("list", $list);
// 변수타입
$num = array(100,'200');
setScriptVar("num",$num);
?>
<script>
//alert(market); // I Like Fruit!
//alert(fruit[0]+"/"+fruit[2][0]); // Apple/Tomato
//alert(list.like+"/"+list.buy[0]); // Pineapple/Apple
//alert(num[0] + 10); // 110
//alert(num[1] + 10); // 20010
</script>
게시판 목록
팁게시판
질문은 상단의 QA에서 해주시기 바랍니다.
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 5327 |
GINUSSOFT
|
12년 전 | 5107 | |
| 5326 | 12년 전 | 6180 | ||
| 5325 | 12년 전 | 1882 | ||
| 5324 |
|
12년 전 | 1666 | |
| 5323 |
CTOMAN
|
12년 전 | 1942 | |
| 5322 | 12년 전 | 1732 | ||
| 5321 | 12년 전 | 2407 | ||
| 5320 | 12년 전 | 3637 | ||
| 5319 | 12년 전 | 1571 | ||
| 5318 |
프로프리랜서
|
12년 전 | 1325 | |
| 5317 |
프로프리랜서
|
12년 전 | 964 | |
| 5316 |
프로프리랜서
|
12년 전 | 1215 | |
| 5315 |
프로프리랜서
|
12년 전 | 1127 | |
| 5314 |
프로프리랜서
|
12년 전 | 1341 | |
| 5313 |
프로프리랜서
|
12년 전 | 1934 | |
| 5312 |
프로프리랜서
|
12년 전 | 1499 | |
| 5311 |
프로프리랜서
|
12년 전 | 1337 | |
| 5310 |
프로프리랜서
|
12년 전 | 3880 | |
| 5309 |
프로프리랜서
|
12년 전 | 1496 | |
| 5308 | 12년 전 | 1152 | ||
| 5307 | 12년 전 | 5122 | ||
| 5306 | 12년 전 | 2493 | ||
| 5305 |
오늘도망했다
|
12년 전 | 2227 | |
| 5304 |
senseme
|
12년 전 | 3319 | |
| 5303 | 12년 전 | 3599 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기