如何将php数组转换为json格式
例如,您好,我有下面的json格式。我需要生成一个php数组来构建这个json如何将php数组转换为json格式,php,arrays,json,Php,Arrays,Json,例如,您好,我有下面的json格式。我需要生成一个php数组来构建这个json { "posts": [ { "detial" : { "titel": "web design", "date-publish" : "2015-02-08", "pic-thumbnail" : "small-web-desing-jpg", "pic-medium" : "medium-web-desing-jpg"
{
"posts":
[
{
"detial" : {
"titel": "web design",
"date-publish" : "2015-02-08",
"pic-thumbnail" : "small-web-desing-jpg",
"pic-medium" : "medium-web-desing-jpg"
},
"content" : {
"description" : "description web design post",
"content" : "web design is modern technology in world"
}
}
]
}
请展示一个PHP数组的例子,构建这个json很容易 只需使用PHP的
json\u decode()
函数即可实现
最后,将json_encode()返回数组保存到变量
输出:
Array
(
[posts] => Array
(
[0] => Array
(
[detial] => Array
(
[titel] => web design
[date-publish] => 2015-02-08
[pic-thumbnail] => small-web-desing-jpg
[pic-medium] => medium-web-desing-jpg
)
[content] => Array
(
[description] => description web design post
[content] => web design is modern technology in world
)
)
)
)
$results = json_decode('{
"posts": [{
"detial" : {
"titel": "web design",
"date-publish" : "2015-02-08",
"pic-thumbnail" : "small-web-desing-jpg",
"pic-medium" : "medium-web-desing-jpg"
},
"content" : {
"description" : "description web design post",
"content" : "web design is modern technology in world"
}
}]
}', true);
echo "<pre>".print_r($results, true)."</pre>";
PHP代码:
Array
(
[posts] => Array
(
[0] => Array
(
[detial] => Array
(
[titel] => web design
[date-publish] => 2015-02-08
[pic-thumbnail] => small-web-desing-jpg
[pic-medium] => medium-web-desing-jpg
)
[content] => Array
(
[description] => description web design post
[content] => web design is modern technology in world
)
)
)
)
$results = json_decode('{
"posts": [{
"detial" : {
"titel": "web design",
"date-publish" : "2015-02-08",
"pic-thumbnail" : "small-web-desing-jpg",
"pic-medium" : "medium-web-desing-jpg"
},
"content" : {
"description" : "description web design post",
"content" : "web design is modern technology in world"
}
}]
}', true);
echo "<pre>".print_r($results, true)."</pre>";
$results=json\u decode('{
“员额”:[{
“细节”:{
“titel”:“网页设计”,
“发布日期”:“2015-02-08”,
“图片缩略图”:“小型网页设计jpg”,
“pic媒体”:“媒体网页设计jpg”
},
“内容”:{
“说明”:“说明网页设计帖子”,
“内容”:“网页设计是世界上的现代技术”
}
}]
}",对),;
echo“”。打印($results,true)。“”;
使用PHP函数json_decode()生成PHP数组
$jsonData=json\u解码('{
“员额”:[{
“细节”:{
“titel”:“网页设计”,
“发布日期”:“2015-02-08”,
“图片缩略图”:“小型网页设计jpg”,
“pic媒体”:“媒体网页设计jpg”
},
“内容”:{
“说明”:“说明网页设计帖子”,
“内容”:“网页设计是世界上的现代技术”
}
}]
}",对),;
回声“;
打印(jsonData);
$obj=json\u encode($your\u string\u here,true)代码>