首页 > 解决方案 > 在 Laravel - php 中使用 json_encode() 将收集和数组数据编码为 json 有什么区别?

问题描述

尝试在 laravel - php 中对 collect() 和 array() 类型的一些数据进行 json_encode 时,返回类似的 json 对象。在解码(python - 第三方 API)它区分什么?

但是在编码为json之后,两者看起来都一样

{"type":"collection","data":[{"id":3,"id_ref":32},{"id":4,"id_ref":43}]}

{"type":"array","data":[{"id":3,"id_ref":32},{"id":4,"id_ref":43}]}

编码代码:

json_encode(['type'=>'collection','data'=>collect($array)])

json_encode(['type'=>'array','data'=>$array])

标签: phpjsonpython-3.xlaravelcollections

解决方案


推荐阅读