PHP:5.2中需要json_encode()5.3功能 [英] PHP: need json_encode() 5.3 functionality in 5.2
本文介绍了PHP:5.2中需要json_encode()5.3功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
长话短说,客户端的托管使用的是PHP 5.2.5,我迫切需要使用5.3附带的json_encode()
的JSON_FORCE_OBJECT选项.有人知道一些与此等同的东西吗?请指出来.
Long story short, client's hosting is using php 5.2.5 and i desperately need to use the JSON_FORCE_OBJECT option with json_encode()
that came with 5.3. Does anyone know some equivalent for that purpose? Point it out please.
推荐答案
在5.2.5上编码之前对我来说是铸造对象:
Casting as an object before encoding worked for me on 5.2.5:
$array = array(1,2,3);
echo json_encode($array);
// [0,1,2]
echo json_encode((object)$array);
// {"0":1,"1":2,"2":3}
使用PHP 5.3会产生相同的输出:
And using PHP 5.3 yields the same output:
echo json_encode($array);
// [0,1,2]
echo json_encode($array, JSON_FORCE_OBJECT);
// {"0":1,"1":2,"2":3}
您还可以从另一个线程尝试此解决方案.不确定确切的用例是什么.
You might also try this solution from another thread. Not sure what your exact use-case is.
这篇关于PHP:5.2中需要json_encode()5.3功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文