更改json键名[使用json_encode生成的json] [英] change json key name [ json generated using json_encode ]
本文介绍了更改json键名[使用json_encode生成的json]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用json_encode()
从数组生成json,它工作正常,但照常使用数组中的key:value.但我只想在json输出中更改键的名称..可以这样做吗?还是我应该准备json键:手动给自己赋值?
I am generating json from an array using json_encode()
, it's working properly, but it uses the key:value from the array, as usual. but I want to change the name of the key only in the json output.. is it possible to do it ? or should I prepare the json key:values myself manually ?
示例:
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
O/P
{"a":1,"b":2,"c":3,"d":4,"e":5}
我想要.
{"foo":1,"something":2,"bar":3,"foo":4,"baz":5}
edit:我无法编辑原始数组..(使用framweork生成)
edit : I cannot edit the original array..( generated using framweork)
推荐答案
仅当您自己重写'm'时.您可以使用:
Only if you rewrite 'm yourself. You could use:
$rewriteKeys = array('a' => 'foo', 'b' => 'something', 'c' => 'bar', 'd' => 'foo', 'e' => 'baz');
$newArr = array();
foreach($arr as $key => $value) {
$newArr[ $rewriteKeys[ $key ] ] = $value;
}
echo json_encode($newArr);
不确定这是否是您的目标.
Not sure if that's what you were aiming for.
这篇关于更改json键名[使用json_encode生成的json]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文