本文整理汇总了PHP中toJson函数的典型用法代码示例。如果您正苦于以下问题:PHP toJson函数的具体用法?PHP toJson怎么用?PHP toJson使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了toJson函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: toJson
/**
* toJson() converts a PHP array to JSON, incldues support for older versions of PHP
* @param Array $arr - Array that is to be converted to JSON
* @return JSON string
*/
function toJson($arr)
{
if (function_exists('json_encode')) {
return json_encode($arr);
}
$parts = array();
$is_list = false;
//Find out if the given array is a numerical array
$keys = array_keys($arr);
$max_length = count($arr) - 1;
if ($keys[0] == 0 and $keys[$max_length] == $max_length) {
//See if the first key is 0 and last key is length - 1
$is_list = true;
for ($i = 0; $i < count($keys); $i++) {
//See if each key correspondes to its position
if ($i != $keys[$i]) {
//A key fails at position check.
$is_list = false;
//It is an associative array.
break;
}
}
}
foreach ($arr as $key => $value) {
if (is_array($value)) {
//Custom handling for arrays
if ($is_list) {
$parts[] = toJson($value);
} else {
$parts[] = '"' . $key . '":' . toJson($value);
}
/* :RECURSION: */
} else {
$str = '';
if (!$is_list) {
$str = '"' . $key . '":';
}
//Custom handling for multiple data types
if (is_numeric($value)) {
$str .= $value;
} elseif ($value === false) {
$str .= 'false';
} elseif ($value === true) {
$str .= 'true';
} else {
$str .= '"' . addslashes($value) . '"';
}
//All other things
// :TODO: Is there any more datatype we should be in the lookout for? (Object?)
$parts[] = $str;
}
}
$json = implode(',', $parts);
if ($is_list) {
return '[' . $json . ']';
}
//Return numerical JSON
return '{' . $json . '}';
//Return associative JSON
}
示例2: uninstall
public static function uninstall()
{
global $db, $setting, $admin_cat;
$info = self::info();
$db->delete($setting['db']['pre'] . "survey");
$db->exec("drop", "table", $setting['db']['pre'] . "survey");
$db->delete($setting['db']['pre'] . "admin_cat", array("file", "like", "survey.php%"));
$db->delete($setting['db']['pre'] . "plugin", array("idx", "=", $info['idx']));
deleteCache("admin_cat");
deleteCache("plugin");
$err = array();
if ($db->GetError($err)) {
showInfo($setting['language']['plugin_err_uninstall'] . "\r\n\t\t\t<br />\r\n\t\t\t<pre>\r\n\t\t\t" . join("\n------------------------\n", $err) . "\r\n\t\t\t</pre>\r\n\t\t\t");
} else {
includeCache("admin_cat");
$admin_cat = toJson($admin_cat, $setting['gen']['charset']);
echo <<<mystep
<script language="javascript">
parent.admin_cat = {$admin_cat};
parent.setNav();
</script>
mystep;
MultiDel(ROOT_PATH . "/" . $setting['path']['cache'] . "/plugin/survey/");
MultiDel(dirname(__FILE__) . "/data/");
MakeDir(dirname(__FILE__) . "/data/");
buildParaList("plugin");
echo showInfo($setting['language']['plugin_uninstall_done'], false);
}
}
示例3: run
public function run($method, $para = array(), $return = "", $charset = "utf-8")
{
$result = "";
if (isset($this->methods[$method])) {
$result = call_user_func_array($this->methods[$method], $para);
}
if (empty($charset)) {
$charset = "utf-8";
}
switch ($return) {
case "j":
case "json":
$result = toJson($result, $charset);
break;
case "x":
case "xml":
$result = '<?xml version="1.0" encoding="' . $charset . '"?>' . "\n<mystep>\n" . toXML($result) . "</mystep>";
header('Content-Type: application/xml; charset=' . $charset);
break;
case "s":
case "string":
$result = toString($result);
break;
default:
break;
}
return $result;
}
示例4: addAnnotation
public function addAnnotation()
{
if ($this->authModel->checkPermissions('add_notes')) {
$vehicleId = $_POST['vehicle_id'];
$text = $_POST['text'];
echo toJson($this->vehicleModel->addVehicleAnnotation($this->authModel->getUserData('id'), $vehicleId, $text));
}
}
示例5: toJson
function toJson($content)
{
//不支持单个文字
if (is_string($content)) {
return urlencode($content);
} elseif (is_array($content)) {
foreach ($content as $key => $val) {
$content[$key] = toJson($val);
}
return urldecode(json_encode($content));
} elseif (is_object($content)) {
$vars = get_object_vars($content);
foreach ($vars as $key => $val) {
$content->{$key} = toJson($val);
}
return urldecode(json_encode($content));
} else {
return urldecode(json_encode($content));
}
}
示例6: build_page
function build_page($method)
{
global $mystep, $req, $db, $tpl, $web_id, $tpl_info, $website, $setting;
$tpl_info['idx'] = "web_subweb_" . ($method == "list" ? "list" : "input");
$tpl_tmp = $mystep->getInstance("MyTpl", $tpl_info);
$tpl_tmp->allow_script = true;
if ($method == "list") {
$db->select($setting['db']['pre'] . "website", "*", "", array("order" => "web_id"));
while ($record = $db->GetRS()) {
HtmlTrans(&$record);
$tpl_tmp->Set_Loop('record', $record);
}
$tpl_tmp->Set_Variable('title', $setting['language']['admin_web_subweb_title']);
global $admin_cat;
$tpl_tmp->Set_Variable("admin_cat", toJson($admin_cat, $setting['gen']['charset']));
$tpl_tmp->Set_Variable("website", toJson($website, $setting['gen']['charset']));
} else {
$tpl_tmp->Set_Variable('title', $method == "add" ? $setting['language']['admin_web_subweb_add'] : $setting['language']['admin_web_subweb_edit']);
if ($method == "edit") {
$record = $db->record($setting['db']['pre'] . "website", "*", array("web_id", "n=", $web_id));
if ($record === false) {
$tpl->Set_Variable('main', showInfo($setting['language']['admin_web_subweb_error'], 0));
$mystep->show($tpl);
$mystep->pageEnd(false);
}
} else {
$record['web_id'] = 0;
$record['name'] = "";
$record['idx'] = "";
$record['host'] = "";
}
$GLOBALS['subweb_idx'] = $record['idx'];
$tpl_tmp->Set_Variables($record);
$setting['watermark']['mode'] = array($setting['watermark']['mode'] & 1 == 1, $setting['watermark']['mode'] & 2 == 2);
$tpl_tmp->Set_Variable('method', $method);
$tpl_tmp->Set_Variable('back_url', $req->getServer("HTTP_REFERER"));
}
$tpl->Set_Variable('main', $tpl_tmp->Get_Content('$db, $setting'));
$db->Free();
unset($tpl_tmp);
$mystep->show($tpl);
return;
}
示例7: build_page
function build_page($method = "")
{
global $mystep, $req, $tpl, $tpl_info, $setting, $idx, $tpl_path, $method;
$fso = $mystep->getInstance("MyFSO");
$tpl_info['idx'] = "web_template";
if ($method != "show") {
$tpl_info['idx'] .= $method == "list" ? "_list" : "_input";
}
$tpl_tmp = $mystep->getInstance("MyTpl", $tpl_info);
if ($method == "show") {
$tpl_tmp->Set_Variable('title', $setting['language']['admin_web_template_title']);
$tpl_tmp->Set_Variable('tpl_idx', $idx);
$tpl_list = $fso->Get_List($tpl_path);
$max_count = count($tpl_list['dir']);
$the_list = array();
for ($i = 0; $i < $max_count; $i++) {
$tpl_list['dir'][$i] = basename($tpl_list['dir'][$i]);
if ($tpl_list['dir'][$i] == "cache" || strpos($tpl_list['dir'][$i], "admin") !== false) {
continue;
}
$tpl_tmp->Set_Loop("tpl_list", array("idx" => $tpl_list['dir'][$i], "img" => is_file($tpl_path . $tpl_list['dir'][$i] . "/sample.jpg") ? "/" . $setting['path']['template'] . "/" . $tpl_list['dir'][$i] . "/sample.jpg" : "/images/noimage.gif"));
$the_list[] = $tpl_list['dir'][$i];
}
$tpl_tmp->Set_Variable('tpl_list', toJson($the_list, $setting['gen']['charset']));
$max_count = count($GLOBALS['website']);
for ($i = 0; $i < $max_count; $i++) {
$setting_sub = getSubSetting($GLOBALS['website'][$i]['web_id']);
$GLOBALS['website'][$i]['tpl'] = $setting_sub['gen']['template'];
$tpl_tmp->Set_Loop("website", $GLOBALS['website'][$i]);
}
} elseif ($method == "list") {
$tpl_tmp->Set_Variable('title', $setting['language']['admin_web_template_title']);
$tpl_tmp->Set_Variable('tpl_idx', $idx);
$tpl_list = $fso->Get_List($tpl_path);
$max_count = count($tpl_list['dir']);
for ($i = 0; $i < $max_count; $i++) {
$tpl_list['dir'][$i] = basename($tpl_list['dir'][$i]);
if ($tpl_list['dir'][$i] == "cache") {
continue;
}
$tpl_tmp->Set_Loop("tpl_list", array("idx" => $tpl_list['dir'][$i], "selected" => $tpl_list['dir'][$i] == $idx ? "selected" : ""));
}
$css_file = ROOT_PATH . "/images/" . $idx . "/style.css";
if (is_file($css_file)) {
$tpl_tmp->Set_Loop("file", array("name" => "style.css", "size" => GetFileSize(filesize($css_file)), "attr" => $fso->Get_Attrib(substr(DecOct(fileperms($css_file)), -3)), "time" => date("Y/m/d H:i:s", filemtime($css_file))));
}
$file_list = $fso->Get_Tree($tpl_path . $idx, false, ".tpl");
foreach ($file_list as $key => $value) {
$curFile = $value;
$curFile['name'] = $key;
$tpl_tmp->Set_Loop("file", $curFile);
}
} else {
$file = array();
$file['idx'] = $idx;
$file['content'] = "";
if ($method == "edit") {
$file['name'] = $req->getGet("file");
if ($file['name'] == "style.css") {
$the_file = ROOT_PATH . "/images/" . $idx . "/style.css";
$file['type'] = "css";
} else {
$the_file = $tpl_path . $idx . "/" . $file['name'];
$file['type'] = "htmlmixed";
}
if (is_file($the_file)) {
$file['content'] = file_get_contents($the_file);
$file['content'] = htmlspecialchars($file['content']);
$file['content'] = str_replace("\t", " ", $file['content']);
}
$tpl_tmp->Set_Variable('title', $setting['language']['admin_web_template_edit']);
} else {
$file['name'] = "";
$tpl_tmp->Set_Variable('title', $setting['language']['admin_web_template_add']);
}
$tpl_tmp->Set_Variable('readonly', $method == "edit" ? "readonly" : "");
$tpl_tmp->Set_Variables($file, "file");
}
$tpl_tmp->Set_Variable('back_url', $req->getServer("HTTP_REFERER"));
$tpl_tmp->Set_Variable('method', $method);
$tpl->Set_Variable('main', $tpl_tmp->Get_Content('$db, $setting'));
unset($tpl_tmp);
$mystep->show($tpl);
return;
}
示例8: json_decode
if (!empty($check_info)) {
$check_info = json_decode($check_info);
$the_file = ROOT_PATH . "/cache/checkfile.php";
if (file_exists($the_file)) {
rename($the_file, $the_file . ".bak");
}
$file_list = $check_info->file_list;
$file_list_md5 = $check_info->file_list_md5;
unset($check_info);
$content = "<?php\n";
$content .= '$file_list = ' . var_export($file_list, true) . ";\n";
$content .= '$file_list_md5 = ' . var_export($file_list_md5, true) . ";\n";
$content .= "?>";
WriteFile($the_file, $content, "wb");
$result = checkFile();
echo toJson($result, $setting['gen']['charset']);
@unlink($the_file);
if (file_exists($the_file . ".bak")) {
rename($the_file . ".bak", $the_file);
}
} else {
echo "error";
}
break;
case "u_update":
$header['Referer'] = "http://" . $req->GetServer("HTTP_HOST");
$header['ms_sign'] = $setting['web']['sign'];
$update_info = GetRemoteContent($setting['gen']['update'] . "?m=u_update&v=" . $ms_version['ver'] . "&cs=" . $setting['gen']['charset'], $header);
if (!empty($update_info)) {
$update_info = preg_replace("/(^|[\r\n]+)([\\w]{0,6})[\r\n]+/", "", $update_info);
$update_info = base64_decode($update_info);
示例9: Array
$tpl_tmp->Set_Variable('sub_page', 'new Array()');
} else {
$sql = $db->buildSel($setting['db']['pre_sub'] . "news_detail", "sub_title, page", array("news_id", "n=", $news_id), array("order" => "page"));
$result = getData($sql, "all", 1200);
$max_count = count($result);
for ($i = 0; $i < $max_count; $i++) {
$result[$i]['url'] = getUrl("read", array($news_id, $cat_idx), $result[$i]['page'], $setting['info']['web']['web_id']);
if ($result[$i]['page'] == $page) {
$result[$i]['selected'] = "selected";
}
$result[$i]['txt'] = sprintf($setting['language']['page_no'], $result[$i]['page']);
if (!empty($result[$i]['sub_title'])) {
$result[$i]['txt'] .= " - " . $result[$i]['sub_title'];
}
}
$tpl_tmp->Set_Variable('sub_page', toJson($result, $setting['gen']['charset']));
$tpl_tmp->Set_Variable('link_all', getUrl("read", array($news_id, $cat_idx), "all", $setting['info']['web']['web_id']));
unset($result);
}
//Prev. Article
$sql = $db->buildSel($setting['db']['pre_sub'] . "news_show", "news_id, cat_id, subject, add_date", array("news_id", "n<", $news_id), array("order" => "news_id desc", "limit" => "1"));
if ($article = getData($sql, "record")) {
if ($cat_info = getParaInfo("news_cat", "cat_id", $article['cat_id'])) {
$cat_idx = $cat_info['cat_idx'];
} else {
$cat_idx = "";
}
$tpl_tmp->Set_Variable('article_prev_link', getUrl("read", array($article['news_id'], $cat_idx), 1, $setting['info']['web']['web_id']));
$tpl_tmp->Set_Variable('article_prev_text', $article['subject']);
} else {
$tpl_tmp->Set_Variable('article_prev_link', "###");
示例10: mysql_query
$aid = $_GET["aid"];
$result = mysql_query("SELECT r.ActivityId, r.Title, r.price, m.Name, m.ImgSrc AS UserImg, r.Status, r.StartDay, DATEDIFF( r.EndDay, r.StartDay ) AS DiffDate, r.ApplyStop, r.Content, rank.rank,o.Join, pk.Watch, pl.place,r.ImgSrc AS TripImg FROM `activityrecord` AS r INNER JOIN activityplan AS p LEFT OUTER JOIN ( SELECT r.ActivityId, COUNT( o.UserId ) AS 'Join' FROM activityrecord AS r LEFT OUTER JOIN orderform AS o ON o.ActivityId = r.ActivityId GROUP BY r.ActivityId ) AS o ON o.ActivityId = r.ActivityId LEFT OUTER JOIN (SELECT r.ActivityId, COUNT( p.UserId ) AS 'Watch' FROM activityrecord AS r LEFT OUTER JOIN pocket AS p ON p.ActivityId = r.ActivityId GROUP BY r.ActivityId ) AS pk ON pk.ActivityId = r.ActivityId LEFT OUTER JOIN (SELECT ActivityId, GROUP_CONCAT( place ORDER BY PlanStep ) AS Place FROM `activityplan` GROUP BY ActivityId ) AS pl ON pl.ActivityId = r.ActivityId LEFT OUTER JOIN (SELECT `UserId` , `Name` , `ImgSrc` FROM `member` ) AS m ON m.UserId= r.UserId LEFT OUTER JOIN (SELECT a.userid, COUNT( c.rank ) AS Rank FROM activityrecord AS a LEFT OUTER JOIN orderform AS b ON b.ActivityId = a.ActivityId LEFT OUTER JOIN rank AS c ON c.OrderId = b.OrderId GROUP BY a.userid) AS rank ON rank.UserId= r.UserId WHERE r.ActivityId = " . $aid . " GROUP BY r.ActivityId");
$record_count = mysql_num_rows($result);
if ($record_count < 1) {
break;
}
$view = view('activity', toJson($result));
echo $view;
$result = mysql_query("SELECT `Title`,`PlanStep`,`place`,`Content`,`ImgSrc` FROM `activityplan` WHERE `ActivityId` = '" . $aid . "' ORDER BY PlanStep");
$record_count = mysql_num_rows($result);
if ($record_count < 1) {
break;
}
$view = view('activity-plan', toJson($result));
echo $view;
$result = mysql_query("SELECT q.UserId, u.Name, u.Imgsrc,q.Question,q.Answer,q.ActivityId,q.CreateDate FROM activityissue AS q INNER JOIN( SELECT UserId,Name,ImgSrc FROM member ) AS u ON u.UserId = q.UserId WHERE `ActivityId` = '" . $aid . "' ORDER BY q.CreateDate DESC");
$record_count = mysql_num_rows($result);
if ($record_count < 1) {
break;
}
$view = view('activity-review', toJson($result));
echo $view;
break;
}
function toJson($result)
{
while ($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
return $jsonString = json_encode($rows);
}
示例11: strrchr
$ext = strrchr($upload->upload_result[$i]['name'], ".");
$name = str_replace($ext, "", $upload->upload_result[$i]['name']);
rename($path_upload . $upload->upload_result[$i]['new_name'], $path_upload . str_replace(".upload", "", str_replace($ext, "", $upload->upload_result[$i]['new_name'])));
$upload->upload_result[$i]['name'] = substrPro($name, 0, 80) . $ext;
$upload->upload_result[$i]['new_name'] = str_replace(".upload", "", $upload->upload_result[$i]['new_name']);
$files[] = array(str_replace($ext, "", $upload->upload_result[$i]['new_name']), $upload->upload_result[$i]['name'], $upload->upload_result[$i]['type'], $_POST['embed'][$i]);
$message[] = $upload->upload_result[$i]['name'] . " - " . $setting['language']['plugin_email_upload_done'];
} else {
$message[] = $upload->upload_result[$i]['name'] . " - " . $setting['language']['plugin_email_upload_failed'] . " - " . $upload->upload_result[$i]['message'];
}
}
$message = implode("\\n", $message);
if (!empty($message)) {
$script = '
alert("' . $message . '");
setAttachment(' . toJson($files, $setting['gen']['charset']) . ');
';
}
}
break;
default:
break;
}
build_page();
$mystep->pageEnd(false);
function build_page()
{
global $mystep, $setting, $script;
$tpl_info = array("idx" => "attachment", "style" => "../plugin/" . basename(realpath(dirname(__FILE__))) . "/tpl/", "path" => ROOT_PATH . "/" . $setting['path']['template']);
$tpl = $mystep->getInstance("MyTpl", $tpl_info);
$Max_size = ini_get('upload_max_filesize');
示例12: PostContactsTransactionalDataUpdate
public function PostContactsTransactionalDataUpdate($collectionName, $importId, ApiJsonData $apiJsonData)
{
$url = sprintf("contacts/transactional-data/%s/%s", $collectionName, $importId);
return new ApiTransactionalData($this->execute($url, 'POST', $apiJsonData > toJson()));
}
示例13: GetImageSize
$new_id = $db->GetInsertId();
if ($new_id != 0) {
$upload->upload_result[0]['att_id'] = $new_id;
if (strpos($upload->upload_result[0]['type'], "image") === 0) {
$upload->MakeDir("{$path_upload}/preview/");
$img_info = GetImageSize("{$path_upload}/" . $upload->upload_result[0]['new_name']);
$the_width = $img_info[0];
$the_height = $img_info[1];
$zoom = 400;
if ($the_width > $zoom) {
$the_height *= $zoom / $the_width;
$the_width = (int) $zoom;
img_thumb($path_upload . "/" . $upload->upload_result[0]['new_name'], $the_width, $the_height, $path_upload . "/preview/" . $upload->upload_result[0]['new_name']);
} else {
copy($path_upload . "/" . $upload->upload_result[0]['new_name'], $path_upload . "/preview/" . $upload->upload_result[0]['new_name']);
}
}
$script .= "parent.document.forms[0].attach_list.value += '{$new_id}|';\n";
$err_msg[] = $upload->upload_result[0]['name'] . " - " . $setting['language']['admin_attachment_upload_done'];
} else {
unlink("{$path_upload}/" . $upload->upload_result[0]['new_name']);
$upload->upload_result[0]['att_id'] = 0;
$upload->upload_result[0]['error'] = 10;
$upload->upload_result[0]['message'] = $setting['language']['admin_attachment_upload_dberr'];
}
} else {
$upload->upload_result[0]['att_id'] = 0;
}
echo toJson($upload->upload_result[0], $setting['gen']['charset']);
/*att_id, name, new_name, type, tmp_name, error, size, message*/
$mystep->pageEnd(false);
示例14: build_list
public static function build_list($news_id, $web_id)
{
global $db, $setting;
$comment_list = array();
$db->select($setting['db']['pre'] . "comment", "*", array(array("news_id", "n=", $news_id), array("web_id", "n=", $web_id, "and")), array("order" => "id asc"));
while ($comment = $db->GetRS($query)) {
HtmlTrans($comment);
$comment['quote_txt'] = "";
if ($comment['quote'] > 0) {
if (isset($comment_list[$comment['quote'] - 1])) {
$quote_comment = $comment_list[$comment['quote'] - 1];
$quote_text = sprintf($setting['language']['plugin_comment_quote'], $comment['quote'], $quote_comment['user_name']);
$comment['quote_txt'] = <<<windy2000
<fieldset>
\t<legend>{$quote_text}</legend>
\t<div>{$quote_comment['quote_txt']}</div>
\t<div><pre>{$quote_comment['comment']}</pre></div>
</fieldset>
windy2000;
}
}
$comment_list[] = $comment;
}
$content = toJson($comment_list, $setting['gen']['charset']);
if (is_null($content)) {
$content = "";
}
WriteFile(ROOT_PATH . "/plugin/comment/cache/" . $web_id . "/" . ceil($news_id / 1000) * 1000 . "/" . $news_id . ".txt", $content, "wb");
return;
}
示例15: define
<?php
$ms_sign = 1;
$etag_expires = 604800;
define('ROOT_PATH', str_replace("\\", "/", realpath(dirname(__FILE__) . "/../")));
require ROOT_PATH . "/include/config.php";
require ROOT_PATH . "/include/parameter.php";
require ROOT_PATH . "/source/function/etag.php";
require ROOT_PATH . "/source/function/global.php";
require ROOT_PATH . "/source/function/web.php";
require ROOT_PATH . "/source/class/abstract.class.php";
require ROOT_PATH . "/source/class/mystep.class.php";
$mystep = new MyStep();
$mystep->pageStart(true);
header('Content-Type: application/x-javascript');
$cache_file = ROOT_PATH . "/" . $setting['path']['cache'] . "script/" . $setting['info']['web']['idx'] . "_setting.js";
if (file_exists($cache_file) && filemtime($cache_file) + $etag_expires > $setting['info']['time_start'] / 1000) {
$result = GetFile($cache_file);
} else {
$result = "";
$result .= "var ms_setting = " . toJson($setting['js'], $setting['gen']['charset']) . ";\n";
$result .= "ms_setting.lang = \"" . $setting['gen']['language'] . "\";";
WriteFile($cache_file, $result, "wb");
}
header("Accept-Ranges: bytes");
header("Accept-Length: " . strlen($result));
echo $result;
$mystep->pageEnd(false);
?>
D:/Website/mystep/aa.txtD:/Website/mystep/aa.txt