<?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년 전
유용한 정보네요
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 6630 |
차가운바람
|
12년 전 | 1164 | |
| 6629 | 12년 전 | 1157 | ||
| 6628 |
put777
|
12년 전 | 642 | |
| 6627 |
|
12년 전 | 698 | |
| 6626 | 12년 전 | 1616 | ||
| 6625 |
|
12년 전 | 718 | |
| 6624 |
미켈란젤로의왼손
|
12년 전 | 485 | |
| 6623 |
|
12년 전 | 1045 | |
| 6622 | 12년 전 | 1392 | ||
| 6621 |
차가운바람
|
12년 전 | 631 | |
| 6620 | 12년 전 | 732 | ||
| 6619 |
|
12년 전 | 969 | |
| 6618 | 12년 전 | 1741 | ||
| 6617 | 12년 전 | 639 | ||
| 6616 |
차가운바람
|
12년 전 | 894 | |
| 6615 | 12년 전 | 2853 | ||
| 6614 | 12년 전 | 614 | ||
| 6613 |
강명구베드로
|
12년 전 | 531 | |
| 6612 |
|
12년 전 | 426 | |
| 6611 | 12년 전 | 1206 | ||
| 6610 | 12년 전 | 1022 | ||
| 6609 | 12년 전 | 3679 | ||
| 6608 |
|
12년 전 | 831 | |
| 6607 | 12년 전 | 694 | ||
| 6606 | 12년 전 | 566 | ||
| 6605 | 12년 전 | 457 | ||
| 6604 | 12년 전 | 976 | ||
| 6603 |
geektoo
|
12년 전 | 703 | |
| 6602 | 12년 전 | 842 | ||
| 6601 | 12년 전 | 364 | ||
| 6600 | 12년 전 | 408 | ||
| 6599 | 12년 전 | 554 | ||
| 6598 | 12년 전 | 938 | ||
| 6597 | 12년 전 | 942 | ||
| 6596 | 12년 전 | 425 | ||
| 6595 | 12년 전 | 794 | ||
| 6594 | 12년 전 | 4576 | ||
| 6593 | 12년 전 | 2580 | ||
| 6592 | 12년 전 | 829 | ||
| 6591 | 12년 전 | 588 | ||
| 6590 |
|
12년 전 | 1397 | |
| 6589 | 12년 전 | 770 | ||
| 6588 |
GINUSSOFT
|
12년 전 | 5131 | |
| 6587 | 12년 전 | 6215 | ||
| 6586 | 12년 전 | 1050 | ||
| 6585 | 12년 전 | 807 | ||
| 6584 | 12년 전 | 469 | ||
| 6583 |
|
12년 전 | 985 | |
| 6582 | 12년 전 | 871 | ||
| 6581 | 12년 전 | 841 | ||
| 6580 | 12년 전 | 619 | ||
| 6579 |
알랑가몰라
|
12년 전 | 933 | |
| 6578 | 12년 전 | 1335 | ||
| 6577 | 12년 전 | 1504 | ||
| 6576 |
경dragon
|
12년 전 | 775 | |
| 6575 | 12년 전 | 1890 | ||
| 6574 | 12년 전 | 690 | ||
| 6573 | 12년 전 | 989 | ||
| 6572 |
|
12년 전 | 1670 | |
| 6571 |
CTOMAN
|
12년 전 | 1966 | |
| 6570 | 12년 전 | 1737 | ||
| 6569 | 12년 전 | 1895 | ||
| 6568 | 12년 전 | 2409 | ||
| 6567 | 12년 전 | 1033 | ||
| 6566 |
lainfox
|
12년 전 | 1536 | |
| 6565 | 12년 전 | 3657 | ||
| 6564 |
제주프라이스
|
12년 전 | 1542 | |
| 6563 | 12년 전 | 1576 | ||
| 6562 |
프로프리랜서
|
12년 전 | 1353 | |
| 6561 |
프로프리랜서
|
12년 전 | 971 | |
| 6560 |
프로프리랜서
|
12년 전 | 1230 | |
| 6559 |
프로프리랜서
|
12년 전 | 1143 | |
| 6558 |
프로프리랜서
|
12년 전 | 1361 | |
| 6557 |
프로프리랜서
|
12년 전 | 1984 | |
| 6556 |
프로프리랜서
|
12년 전 | 1531 | |
| 6555 |
프로프리랜서
|
12년 전 | 1372 | |
| 6554 |
프로프리랜서
|
12년 전 | 3895 | |
| 6553 |
프로프리랜서
|
12년 전 | 1516 | |
| 6552 | 12년 전 | 850 | ||
| 6551 |
왕초보sasa
|
12년 전 | 1545 | |
| 6550 |
왕초보sasa
|
12년 전 | 640 | |
| 6549 |
왕초보sasa
|
12년 전 | 913 | |
| 6548 | 12년 전 | 1303 | ||
| 6547 | 12년 전 | 1200 | ||
| 6546 | 12년 전 | 5163 | ||
| 6545 | 12년 전 | 2523 | ||
| 6544 |
AnnieK
|
12년 전 | 1786 | |
| 6543 |
베르무트7
|
12년 전 | 648 | |
| 6542 |
오늘도망했다
|
12년 전 | 2236 | |
| 6541 | 12년 전 | 820 | ||
| 6540 | 12년 전 | 1141 | ||
| 6539 | 12년 전 | 853 | ||
| 6538 |
senseme
|
12년 전 | 3380 | |
| 6537 | 12년 전 | 782 | ||
| 6536 | 12년 전 | 3670 | ||
| 6535 | 12년 전 | 1353 | ||
| 6534 | 12년 전 | 1624 | ||
| 6533 | 12년 전 | 2246 | ||
| 6532 |
냐옹이사범
|
12년 전 | 2308 | |
| 6531 | 12년 전 | 571 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기