php数组转json对象和json数组php中数组转json的规则是:当没有指定索引(0~n)时会转换为json数组,⽽指定了索引会转换为json对象。
PHP的数组在转JSON的时候,如果索引连续,则转成数组。如果索引不连续,则会转成对象
1、没有指定索引的情况:
转换为json:
2、有指定索引的情况:
转换为json:
1 2 3 4 5 6 7{
"a": "a", "b": "b", "c": "c", "d": "d", "e": "e" }
3、默认索引,但是索引不连续,也会转成对象【unset() 做数组处理时会使默认索引丢失,可以使⽤array_values()初始化索引】转换json:
1 2 3 4 5 6 7{
"0": "a", "1": "b", "3": "c", "4": "d", "5": "e" }
$attr = array("a","b","c","d","e"); ["a","b","c","d","e"]
$attr = array("a"=>"a","b"=>"b","c"=>"c","d"=>"d","e"=>"e"); $attr = array("0"=>"a","1"=>"b","3"=>"c","4"=>"d","5"=>"e");json转换对象
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论