如何json_en code PHP数组,但不包括引号键 [英] How to json_encode php array but the keys without quotes
问题描述
我想绘制(与 FLOT )有一些数据的饼图
VAR数据=<?PHP的回声json_en code($数据)>
结果我从得到是这样的:
VAR数据= [
{标签:CREAR Usuario,数据:2},
{标签:我现在presoras,数据:1},
{标签:邮报Problema,数据:1},
{标签:Requisicion EQUIPO,数据:1},
{标签:网络锡蒂奥,数据:1}
]
这里的问题是我需要的标签
和数据
不包括引号,我已经尝试过 json_en code($的数据,JSON_NUMERIC_CHECK);
但仅从数字去掉引号
下面的格式是什么,我需要:
VAR数据= [
{标签:CREAR Usuario,数据:2},...
首先,你必须产生在PHP阵列所以数据的值是整数,而不是字符串:
我效仿你的数组从json_en code(),我猜它看起来像这样(或应该):
$阵列=阵列(
阵列(标签=>中CREAR Usuario,数据=> 2),
阵列(标签=>中林presoras,数据=> 1)
阵列(标签=>中Problema邮报,数据=> 1)
阵列(标签=>中Requisicion EQUIPO,数据=> 1)
阵列(标签=>中锡蒂奥网络,数据=> 1)
); $数据= json_en code($数组);
- 注意,2和1的是不带引号的,所以这种方式,他们是整数,这是非常重要的。
然后你在Javascript想念的JSON.parse()来实际上使该输出成JSON对象:
<脚本>
VAR数据='< PHP的echo $的数据; ?>';
变种JSON = JSON.parse(数据);
的console.log(JSON);
的console.log(JSON [0]);
< / SCRIPT>
- 请注意,VAR数据=是单引号,让你赶上从PHP的回声作为一个String
在执行console.log()的输出这对我来说:
[对象,对象,目标,对象,对象] //首先执行console.log():用5个对象一个对象。
对象{标号:CREAR Usuario,数据:2} // secons控制台日志(JSON [0])与所述第一对象
看起来你需要什么,对吗?
I'm trying to plot (with Flot) a pie chart with some data
var data = <?php echo json_encode($data)?>
The result I get from that is this:
var data = [
{"label":"Crear Usuario", "data":"2"},
{"label":"Impresoras", "data":"1"},
{"label":"Problema Correo", "data":"1"},
{"label":"Requisicion Equipo", "data":"1"},
{"label":"Sitio Web", "data":"1"}
]
The problem here is that I need the label
and data
without the quotes, I already tried json_encode($data, JSON_NUMERIC_CHECK);
but only removes the quotes from the numbers.
The following format is what I need:
var data = [
{label:"Crear Usuario",data:2}, ...
First, you have to generate your array in php so the data's value are integers, not strings:
I emulated your array from your json_encode(), I guess it looks like this (or it should):
$array = array(
array("label" => "Crear Usuario", "data" => 2),
array("label" => "Impresoras", "data" => 1),
array("label" => "Problema Correo", "data" => 1),
array("label" => "Requisicion Equipo", "data" => 1),
array("label" => "Sitio Web", "data" => 1)
);
$data = json_encode($array);
- Notice that the 2 and 1's are unquoted, so this way they are integers, this is important.
Then you are missin in Javascript the JSON.parse() to actually make that output into a json object:
<script>
var data = '<?php echo $data; ?>';
var json = JSON.parse(data);
console.log(json);
console.log(json[0]);
</script>
- Notice that var data = ... is SINGLE QUOTED, so you catch the echo from php as a String
The console.log()'s output this for me:
[Object, Object, Object, Object, Object] // First console.log(): one object with the 5 Objects.
Object {label: "Crear Usuario", data: 2} // secons console log (json[0]) with the first object
Looks like what you need, am I right?
这篇关于如何json_en code PHP数组,但不包括引号键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!