本文整理汇总了PHP中functions::json_encode方法的典型用法代码示例。如果您正苦于以下问题:PHP functions::json_encode方法的具体用法?PHP functions::json_encode怎么用?PHP functions::json_encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类functions
的用法示例。
在下文中一共展示了functions::json_encode方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
function run()
{
/**@var tf_sat $sat */
$sat = core::module('sat');
$site = $sat->get_current_site();
$json_file = loader::get_public('assets/' . $site->domain . '.json');
$last_mod = @filemtime($json_file);
$tree_last_mod = 0;
if ($last_mod) {
$last_node = $sat->get_node_handle()->set_where('site_id = %d', $sat->get_current_site_id())->set_order('updated_at DESC')->load_first();
$tree_last_mod = $last_node ? $last_node->updated_at : 0;
core::dprint(__METHOD__ . ': cached');
// uptodate
if ($tree_last_mod <= $last_mod) {
$this->renderer->set_ajax_answer(file_get_contents($json_file))->ajax_flush();
return;
}
}
core::dprint(__METHOD__ . ': fetch!');
$tree = $sat->get_current_site_tree();
$allowedKeys = array('title', 'url');
array_walk($tree, function (&$v) use($allowedKeys) {
$v = array_intersect_key($v, array_flip($allowedKeys));
});
$tree = array_values($tree);
// cache
file_put_contents($json_file, functions::json_encode($tree));
$this->renderer->set_ajax_answer($tree)->ajax_flush();
}
示例2: as_json
/**
* Convert to json
* @return string json
*/
function as_json($fields = false, $assoc = false)
{
$out = array();
if (!empty($this->items)) {
foreach ($this->items as $id => $v) {
$row = $v->as_json($fields);
if ($assoc) {
$out[$id] = $row;
} else {
$out[] = $row;
}
}
}
return functions::json_encode($out);
}
示例3: output_ajax
/**
* Triggers in ajax mode call
* Template assign through: @see get_main_template()
* called from @see core::shutdown
*/
public function output_ajax($is_return = false)
{
/*
//disabled in core
if ($console = core::lib('console')) {
$console->disable();
}
*/
$this->output_begin(array('in_ajax' => true));
// output_ajax conflict in editor
// @todo test this
if (loader::in_ajax() === 'json') {
$this->set_ajax_type('json');
}
if (core::in_editor()) {
$this->output_editor();
}
$tpl = $this->get_main_template();
if ($tpl) {
$tpl = substr($tpl, 0, -4);
// output_end will append .TPL
}
if (!$is_return && self::AJAX_JSON == @$this->ajax_answer['type']) {
$this->content_type_header('application/json', 'utf-8');
}
/**
* JSON answer
*/
if (!empty($tpl) && self::AJAX_JSON == @$this->ajax_answer['type']) {
// parse template, if any
$buffer = $this->output_end($tpl, true);
// output ajax json data
$return = is_array($this->ajax_answer['data']) ? $this->ajax_answer['data'] : array();
// if rendered with template
if (isset($buffer)) {
$return['data'] = $buffer;
}
// send message
if (empty($tpl) && !empty($this->_message['message'])) {
$return['message'] = $this->_message['message'];
}
echo json_encode($return);
} elseif (empty($tpl)) {
// allow prepared json
echo self::AJAX_JSON == $this->ajax_answer['type'] ? is_string($this->ajax_answer['data']) ? $this->ajax_answer['data'] : functions::json_encode($this->ajax_answer['data']) : @$this->ajax_answer['data']['data'];
} else {
// plain output
$this->output_end($tpl);
}
}