<?php
// PHP변수 스크립트에서 쉽게 사용하기
function setScriptVar($name, $obj) {
global $script;
$script = "<script language='javascript'>";
// 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;
}
$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."';";
}
}
}
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);
$market = "I Like Fruit!";
setScriptVar("market", $market);
// 배열
$fruit = array('Apple','Pineapple',array('Tomato','Banana'));
setScriptVar("fruit", $fruit);
$fruit = array('Apple','Pineapple',array('Tomato','Banana'));
setScriptVar("fruit", $fruit);
// 오브젝트와 배열
$list->like = 'Pineapple';
$list->hate = 'Tomato';
$list->buy = array('Apple','Pineapple');
setScriptVar("list", $list);
$list->like = 'Pineapple';
$list->hate = 'Tomato';
$list->buy = array('Apple','Pineapple');
setScriptVar("list", $list);
// 변수타입
$num = array(100,'200');
setScriptVar("num",$num);
?>
$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>
//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>
댓글 2개
12년 전
감사해요 ㅎㅎ
takumi22
12년 전
유용한 정보네요
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 6130 |
gender
|
12년 전 | 1092 | |
| 6129 | 12년 전 | 1322 | ||
| 6128 |
|
12년 전 | 3461 | |
| 6127 | 12년 전 | 695 | ||
| 6126 |
|
12년 전 | 2208 | |
| 6125 |
|
12년 전 | 4884 | |
| 6124 | 12년 전 | 660 | ||
| 6123 | 12년 전 | 3843 | ||
| 6122 | 12년 전 | 1017 | ||
| 6121 | 12년 전 | 3751 | ||
| 6120 | 12년 전 | 908 | ||
| 6119 | 12년 전 | 1770 | ||
| 6118 | 12년 전 | 929 | ||
| 6117 | 12년 전 | 2149 | ||
| 6116 | 12년 전 | 7942 | ||
| 6115 | 12년 전 | 1919 | ||
| 6114 |
|
12년 전 | 1705 | |
| 6113 | 12년 전 | 1522 | ||
| 6112 | 12년 전 | 594 | ||
| 6111 | 12년 전 | 2111 | ||
| 6110 | 12년 전 | 1850 | ||
| 6109 | 12년 전 | 640 | ||
| 6108 | 12년 전 | 1198 | ||
| 6107 | 12년 전 | 622 | ||
| 6106 | 12년 전 | 904 | ||
| 6105 | 12년 전 | 1191 | ||
| 6104 | 12년 전 | 3595 | ||
| 6103 | 12년 전 | 2121 | ||
| 6102 | 12년 전 | 2315 | ||
| 6101 | 12년 전 | 3677 | ||
| 6100 | 12년 전 | 3483 | ||
| 6099 | 12년 전 | 3152 | ||
| 6098 | 12년 전 | 4024 | ||
| 6097 | 12년 전 | 1015 | ||
| 6096 | 12년 전 | 5981 | ||
| 6095 | 12년 전 | 1369 | ||
| 6094 | 12년 전 | 1224 | ||
| 6093 | 12년 전 | 3406 | ||
| 6092 | 12년 전 | 3053 | ||
| 6091 | 12년 전 | 5194 | ||
| 6090 | 12년 전 | 2703 | ||
| 6089 | 12년 전 | 3326 | ||
| 6088 | 12년 전 | 1015 | ||
| 6087 | 12년 전 | 847 | ||
| 6086 | 12년 전 | 2018 | ||
| 6085 |
|
12년 전 | 806 | |
| 6084 |
웹디자인되고파
|
12년 전 | 2216 | |
| 6083 | 12년 전 | 1507 | ||
| 6082 | 12년 전 | 1111 | ||
| 6081 | 12년 전 | 2099 | ||
| 6080 |
Stiven
|
12년 전 | 2312 | |
| 6079 |
프로프리랜서
|
12년 전 | 1318 | |
| 6078 |
프로프리랜서
|
12년 전 | 786 | |
| 6077 |
프로프리랜서
|
12년 전 | 1342 | |
| 6076 |
프로프리랜서
|
12년 전 | 830 | |
| 6075 |
프로프리랜서
|
12년 전 | 1223 | |
| 6074 | 12년 전 | 3854 | ||
| 6073 | 12년 전 | 3949 | ||
| 6072 | 12년 전 | 1376 | ||
| 6071 | 12년 전 | 6992 | ||
| 6070 | 12년 전 | 7574 | ||
| 6069 | 12년 전 | 2320 | ||
| 6068 | 12년 전 | 3868 | ||
| 6067 |
smwkd
|
12년 전 | 632 | |
| 6066 | 12년 전 | 3656 | ||
| 6065 | 12년 전 | 3486 | ||
| 6064 | 12년 전 | 2720 | ||
| 6063 | 12년 전 | 2840 | ||
| 6062 | 12년 전 | 2371 | ||
| 6061 | 12년 전 | 2276 | ||
| 6060 | 12년 전 | 5184 | ||
| 6059 | 12년 전 | 2814 | ||
| 6058 | 12년 전 | 3134 | ||
| 6057 | 12년 전 | 2261 | ||
| 6056 | 12년 전 | 6841 | ||
| 6055 | 12년 전 | 2581 | ||
| 6054 | 12년 전 | 3435 | ||
| 6053 | 12년 전 | 2328 | ||
| 6052 | 12년 전 | 4820 | ||
| 6051 | 12년 전 | 3712 | ||
| 6050 | 12년 전 | 2555 | ||
| 6049 | 12년 전 | 2245 | ||
| 6048 |
|
12년 전 | 1297 | |
| 6047 | 12년 전 | 3441 | ||
| 6046 | 12년 전 | 4095 | ||
| 6045 | 12년 전 | 3455 | ||
| 6044 | 12년 전 | 5331 | ||
| 6043 | 12년 전 | 1651 | ||
| 6042 | 12년 전 | 1282 | ||
| 6041 | 12년 전 | 5165 | ||
| 6040 | 12년 전 | 939 | ||
| 6039 | 12년 전 | 3412 | ||
| 6038 | 12년 전 | 3412 | ||
| 6037 | 12년 전 | 2992 | ||
| 6036 | 12년 전 | 3341 | ||
| 6035 | 12년 전 | 2867 | ||
| 6034 | 12년 전 | 2845 | ||
| 6033 | 12년 전 | 2866 | ||
| 6032 | 12년 전 | 2854 | ||
| 6031 | 12년 전 | 2880 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기