Karp

PHP json 中 数值索引自动排序问题
PHP json_encode函数 数组中键值为数值索引时编码为对象格式而不是数组格式<?php $a...
扫描右侧二维码阅读全文
28
2020/07

PHP json 中 数值索引自动排序问题

PHP json_encode函数 数组中键值为数值索引时编码为对象格式而不是数组格式

<?php 
 
$arr=array(
   '0'=>'张三',
   '1'=>'李四'
)
 
echo json_encode($arr);
 
//结果为
['张三','李四']
 
//不是想要的结果 预期的结果是
//{"0":"张三","1":"李四"}
 
 
//解决方案
$arr=new ArrayObject($arr);
 
echo json_encode($arr);

因为大多数接口 返回都是json 格式 PHP是使用json_encode实现的, 但是数值索引会自动排序.
算是长知识了 json 的坑

最后修改:2020 年 07 月 28 日 11 : 43 PM

1 条评论

  1. planck

    博主大大,请教一下为何你的页面滚动如此顺滑,我尝试过 body, html { scroll-behavior:smooth; },可是一点效果也没有。

发表评论