How can I force PHP's json_encode integer values to be encoded as String?

后端 未结 2 373
青春惊慌失措
青春惊慌失措 2021-01-24 11:47

I\'m using PHP v5.6.

As i read that php json_encode function is automatically converting int to string. But not in my case. Json_encode is still return it to int not str

相关标签:
2条回答
  • 2021-01-24 12:24
    json_encode(['code' => 200, 'results' => [id=> strval(1)]]);
    

    With strval() php will return

    The string value of var.

    0 讨论(0)
  • 2021-01-24 12:35

    In the link posted by Thomas in comments, one user suggests that you do this:

    $data = json_encode(array_map('strval', $data));
    

    This might not be the most efficient in terms of performace though, since every entry on the array will pass through the strval function.

    0 讨论(0)
提交回复
热议问题