json_encode(JSON_FORCE_OBJECT)
<?php
/*
json_encode(JSON_FORCE_OBJECT)
JSONのオブジェクトに強制的に変換する方法です。
*/
$ary1=array(1,2,3);
$ary2=array(array(4,5,6));
$ary3=array("red"=>1, "blue"=>2, "yellow"=>3);
print "ary1:" . json_encode($ary1,JSON_FORCE_OBJECT) . "<br />";
print "ary2:" . json_encode($ary2,JSON_FORCE_OBJECT) . "<br />";
print "ary3:" . json_encode($ary3,JSON_FORCE_OBJECT) . "<br />";
/*
[出力結果]
ary1:{"0":1,"1":2,"2":3}
ary2:{"0":{"0":4,"1":5,"2":6}}
ary3:{"red":1,"blue":2,"yellow":3}
*/
?>
|
|