如何在php中将json字符串转换为数组? [英] how to convert json string to array in php?
本文介绍了如何在php中将json字符串转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个json格式,想要将此格式转换为我的自定义格式.请帮助我使用PHP进行此转换.请帮帮我,我如何构造上述JSON数组格式.这是我的格式:
I have a json format and want to convert this to my customized format. Please help me for this conversion using PHP. Please help me out, how can i construct the above mentioned JSON array format. Here is my format:
[{"label":"Food & Drinks","data":"2"},{"label":"Lifestyle","data":"1"}]
并想要这种格式的结果:
And want result in this format:
[["Food & Drinks", 2],["Lifestyle", 1]]
推荐答案
尝试以下解决方案:
$json = '[{"label":"Food & Drinks","data":"2"},{"label":"Lifestyle","data":"1"}]';
$json_array = json_decode($json, true);
$new_array = array();
foreach($json_array as $arr){
$new_array[] = array_values($arr);
}
print_r($new_array);
echo json_encode($new_array);
输出:
Array
(
[0] => Array
(
[0] => Food & Drinks
[1] => 2
)
[1] => Array
(
[0] => Lifestyle
[1] => 1
)
)
[["Food & Drinks","2"],["Lifestyle","1"]]
有关更多详细信息,请参见 PHP:json_decode
for more detail have alook at PHP: json_decode
这篇关于如何在php中将json字符串转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文