[英]PHP array to json format
I am trying to create a chart based on some php data i got in an array, if I run the following 我试图根据我在数组中获得的一些PHP数据创建图表,如果我运行以下命令
<?php
$out = array_values($periodi);
echo json_encode($out, JSON_PRETTY_PRINT);
?>
I get this json format 我得到这个json格式
[
"Francia",
"Italia",
"Italia",
"Germania",
"Afghanistan",
"Italia"
]
While what I am looking for is to get a format like this. 虽然我正在寻找的是获得这样的格式。 Regardless of different values how do I add a propriety name in roder to get something along those lines? 不管值是多少,我如何在Roder中添加一个专有名称以沿这些路线获取内容?
var data = [{
"name": "Tokyo",
"data": 3.0
}, {
"name": "NewYork",
"data": 2.0
}, {
"name": "Berlin",
"data": 3.5
}, {
"name": "London",
"data": 1.5
}];
print_r($periodi); print_r($ periodi);
Array ( [0] => Moderno [1] => Contemporaneo [2] => Contemporaneo [3] => Contemporaneo [4] => Contemporaneo ) 数组([0] => Moderno [1] => Contemporaneo [2] => Contemporaneo [3] => Contemporaneo [4] => Contemporaneo)
I have another array with different data but still the format is wrong: 我有另一个具有不同数据的数组,但是格式仍然错误:
Array ( [francese] => Array ( [maschio] => Array ( [0] => 1 ) [femmina] => Array ( [0] => 1 ) ) [chimica] => Array ( [maschio] => Array ( [0] => 1 [1] => 1 ) ) [fisica] => Array ( [maschio] => Array ( [0] => 1 [1] => 1 ) [femmina] => Array ( [0] => 1 ) ) [scienze] => Array ( [maschio] => Array ( [0] => 1 ) ) [inglese] => Array ( [maschio] => Array ( [0] => 1 ) ) [spagnolo] => Array ( [maschio] => Array ( [0] => 1 ) ) [italiano] => Array ( [femmina] => Array ( [0] => 1 ) ) ) Array([francese] => Array([maschio] => Array([0] => 1)[femmina] => Array([0] => 1))[chimica] => Array([maschio] => Array([0] => 1 [1] => 1))[fisica] => Array([maschio] => Array([0] => 1 [1] => 1)[femmina] => Array( [0] => 1))[scienze] => Array([maschio] => Array([0] => 1))[inglese] => Array([maschio] => Array([0] => 1 ))[spagnolo] =>数组([maschio] =>数组([0] => 1))[italiano] =>数组([femmina] => Array([0] => 1))))
{
"maschio": [
true
],
"femmina": [
true
]
},
{
"maschio": [
true,
true
]
},
{
"maschio": [
true,
true
],
"femmina": [
true
]
},
{
"maschio": [
true
]
},
{
"maschio": [
true
]
},
{
"maschio": [
true
]
},
{
"femmina": [
true
]
}
]
$jsonArr = array();
foreach($periodi as $key => $val){
$jsonArr[$key]['name'] = $val;
$jsonArr[$key]['data'] = $key; //there is no source for priority
//in your array so i just put $key as priority variable
}
json_encode($jsonArr);
You should make your array like this ... 您应该像这样使数组...
Array
(
[0] => Array
(
['name'] => 'Tokyo'
['data'] => 3
)
[1] => Array
(
['name'] => 'NewYork'
['data'] => 2
)
)
How to make the array ? 如何制作数组?
$final_array = array();
foreach($data as $key=>$value)
{
$final_array[]=array(
"name"=>$key,
"data"=>$value
);
}
Then you just need to json_encode
the $final_array
. 然后,您只需要对$final_array
进行json_encode
$final_array
。
<?php
$array=
[
"Moderno",
"Contemporaneo",
"Contemporaneo",
"Contemporaneo",
"Contemporaneo"
];
$array=array_count_values($array);
$result=array();
foreach($array as $key => $value)
{
$result[]=array("name"=>$key,"data"=>$value);
}
print_r(json_encode($result));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.