如何强制将PHP的json_encode整数值编码为String? [英] How can I force PHP's json_encode integer values to be encoded as String?

查看:204
本文介绍了如何强制将PHP的json_encode整数值编码为String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP v5.6.

I'm using PHP v5.6.

当我读到php json_encode函数自动将int转换为字符串时.但就我而言不是. Json_encode仍将其返回给int而不是字符串.

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 string.

例如:

json_encode(['code' => 200, 'results' => [id=> 1]]);

我的预期结果全都变成了字符串.但是我得到的是

my expected results is all become a string. but what i get is

{"code":200,"results":{"id": 1}}

预期输出:

{"code":"200","results":{"id": "1"}}

如何在不为每个值使用"的情况下将所有结果更改为字符串?

How can i change all the result become string without using "" for every value?.

NB:结果数组基于查询.

NB: results array is based on query.

推荐答案

在Thomas发表的评论链接中,一个用户

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

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

这在性能方面可能不是最有效的,因为数组上的每个条目都将通过strval函数.

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

这篇关于如何强制将PHP的json_encode整数值编码为String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆