<?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년 전
유용한 정보네요
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 6530 |
퍼블리셔강
|
12년 전 | 647 | |
| 6529 | 12년 전 | 1303 | ||
| 6528 | 12년 전 | 624 | ||
| 6527 | 12년 전 | 790 | ||
| 6526 | 12년 전 | 6580 | ||
| 6525 | 12년 전 | 549 | ||
| 6524 | 12년 전 | 838 | ||
| 6523 | 12년 전 | 488 | ||
| 6522 | 12년 전 | 535 | ||
| 6521 | 12년 전 | 797 | ||
| 6520 | 12년 전 | 739 | ||
| 6519 | 12년 전 | 1743 | ||
| 6518 |
가슴시린하늘
|
12년 전 | 1723 | |
| 6517 | 12년 전 | 1362 | ||
| 6516 | 12년 전 | 5225 | ||
| 6515 | 12년 전 | 1494 | ||
| 6514 | 12년 전 | 1597 | ||
| 6513 | 12년 전 | 954 | ||
| 6512 | 12년 전 | 1981 | ||
| 6511 | 12년 전 | 1368 | ||
| 6510 | 12년 전 | 3063 | ||
| 6509 |
프로프리랜서
|
12년 전 | 2547 | |
| 6508 |
프로프리랜서
|
12년 전 | 1898 | |
| 6507 |
프로프리랜서
|
12년 전 | 2374 | |
| 6506 |
프로프리랜서
|
12년 전 | 1854 | |
| 6505 |
프로프리랜서
|
12년 전 | 1631 | |
| 6504 | 12년 전 | 1027 | ||
| 6503 | 12년 전 | 1127 | ||
| 6502 |
프로프리랜서
|
12년 전 | 5555 | |
| 6501 |
프로프리랜서
|
12년 전 | 2601 | |
| 6500 |
프로프리랜서
|
12년 전 | 3083 | |
| 6499 |
프로프리랜서
|
12년 전 | 1750 | |
| 6498 |
프로프리랜서
|
12년 전 | 1463 | |
| 6497 | 12년 전 | 808 | ||
| 6496 |
yunkiri486
|
12년 전 | 1809 | |
| 6495 | 12년 전 | 917 | ||
| 6494 | 12년 전 | 1491 | ||
| 6493 | 12년 전 | 2763 | ||
| 6492 |
오늘도망했다
|
12년 전 | 2337 | |
| 6491 |
오늘도망했다
|
12년 전 | 1677 | |
| 6490 |
오늘도망했다
|
12년 전 | 6659 | |
| 6489 |
오늘도망했다
|
12년 전 | 2497 | |
| 6488 |
홈피119
|
12년 전 | 698 | |
| 6487 | 12년 전 | 670 | ||
| 6486 | 12년 전 | 1407 | ||
| 6485 | 12년 전 | 2050 | ||
| 6484 | 12년 전 | 899 | ||
| 6483 | 12년 전 | 717 | ||
| 6482 | 12년 전 | 1341 | ||
| 6481 | 12년 전 | 1468 | ||
| 6480 |
Header
|
12년 전 | 690 | |
| 6479 |
|
12년 전 | 1126 | |
| 6478 |
개초보제이
|
12년 전 | 1394 | |
| 6477 | 12년 전 | 796 | ||
| 6476 | 12년 전 | 1076 | ||
| 6475 | 12년 전 | 3683 | ||
| 6474 | 12년 전 | 1424 | ||
| 6473 | 12년 전 | 961 | ||
| 6472 | 12년 전 | 852 | ||
| 6471 |
AMDbest
|
12년 전 | 1203 | |
| 6470 |
jinmuk
|
12년 전 | 1446 | |
| 6469 |
jinmuk
|
12년 전 | 4250 | |
| 6468 |
jinmuk
|
12년 전 | 5577 | |
| 6467 | 12년 전 | 696 | ||
| 6466 |
jinmuk
|
12년 전 | 2272 | |
| 6465 |
jinmuk
|
12년 전 | 6735 | |
| 6464 |
jinmuk
|
12년 전 | 2408 | |
| 6463 |
jinmuk
|
12년 전 | 9333 | |
| 6462 |
희망과열정
|
12년 전 | 535 | |
| 6461 |
Header
|
12년 전 | 722 | |
| 6460 |
희망과열정
|
12년 전 | 1354 | |
| 6459 |
프리랜서개발자
|
12년 전 | 1677 | |
| 6458 | 12년 전 | 1336 | ||
| 6457 |
jinmuk
|
12년 전 | 1944 | |
| 6456 |
jinmuk
|
12년 전 | 2348 | |
| 6455 |
jinmuk
|
12년 전 | 3204 | |
| 6454 |
jinmuk
|
12년 전 | 2175 | |
| 6453 |
jinmuk
|
12년 전 | 2354 | |
| 6452 |
jinmuk
|
12년 전 | 2316 | |
| 6451 |
jinmuk
|
12년 전 | 1604 | |
| 6450 |
jinmuk
|
12년 전 | 1399 | |
| 6449 |
jinmuk
|
12년 전 | 1045 | |
| 6448 |
jinmuk
|
12년 전 | 1242 | |
| 6447 |
jinmuk
|
12년 전 | 2008 | |
| 6446 |
jinmuk
|
12년 전 | 1564 | |
| 6445 |
jinmuk
|
12년 전 | 1263 | |
| 6444 |
jinmuk
|
12년 전 | 2308 | |
| 6443 |
jinmuk
|
12년 전 | 2045 | |
| 6442 |
jinmuk
|
12년 전 | 1884 | |
| 6441 |
jinmuk
|
12년 전 | 1617 | |
| 6440 |
jinmuk
|
12년 전 | 2316 | |
| 6439 |
jinmuk
|
12년 전 | 1134 | |
| 6438 |
wndProc
|
12년 전 | 779 | |
| 6437 | 12년 전 | 398 | ||
| 6436 |
senseme
|
12년 전 | 544 | |
| 6435 | 12년 전 | 1045 | ||
| 6434 | 12년 전 | 2475 | ||
| 6433 | 12년 전 | 2485 | ||
| 6432 | 12년 전 | 1563 | ||
| 6431 |
밥먹고합시다
|
12년 전 | 1191 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기