admin管理员组

文章数量:1279112

I have to json_encode a PHP array to a JavaScript array. Unfortunately, the jQuery library I am using will not properly process that array if it contains integers instead of strings.

Most of the time, this will produce proper array containing only strings:

json_encode($data)

Even if $data contains just numbers, I usually get this:

["3","7","8"]

Sometimes though, I get results like this (note the zero):

["9691","1792","26","1","4","15",0,"1"]

or this

[16171,15470,10390,7585]

(Note, this is obviously different data to illustrate what's going on). I need to enforce json_encode to treat the array values as strings. I know there's the opposite option JSON_NUMERIC_CHECK which enforces numbers. Does the equivalent really not exist? Seems my only option is to process the array again on the JavaScript end, which, while possible, somewhat breaks the encapsulation of my objects.

I have to json_encode a PHP array to a JavaScript array. Unfortunately, the jQuery library I am using will not properly process that array if it contains integers instead of strings.

Most of the time, this will produce proper array containing only strings:

json_encode($data)

Even if $data contains just numbers, I usually get this:

["3","7","8"]

Sometimes though, I get results like this (note the zero):

["9691","1792","26","1","4","15",0,"1"]

or this

[16171,15470,10390,7585]

(Note, this is obviously different data to illustrate what's going on). I need to enforce json_encode to treat the array values as strings. I know there's the opposite option JSON_NUMERIC_CHECK which enforces numbers. Does the equivalent really not exist? Seems my only option is to process the array again on the JavaScript end, which, while possible, somewhat breaks the encapsulation of my objects.

Share Improve this question edited Apr 9, 2021 at 16:15 mickmackusa 48k13 gold badges93 silver badges161 bronze badges asked Oct 30, 2013 at 15:42 janbjanb 3706 silver badges16 bronze badges 8
  • Maybe this link can help. Regards ! – user12733 Commented Oct 30, 2013 at 15:47
  • @Tino that's the opposite of what I want, as mentioned – janb Commented Oct 30, 2013 at 15:50
  • 1 "Unfortunately the jQuery library I am using will not properly process that array if it contains ints instead of strings" .... if they are actually numbers, then I would say it's your jQuery library that's at fault. But why not just cast them as strings when you pass them into the library? – Spudley Commented Oct 30, 2013 at 15:54
  • 1 “Sometimes though” – that behavior is not as random as you make it sound, but depends on the data types of the values in your array. – C3roe Commented Oct 30, 2013 at 15:57
  • think there is no way. Only way is probably loop through you array and make it a string. stackoverflow./questions/1035634/… – uptownhr Commented Oct 30, 2013 at 15:57
 |  Show 3 more ments

4 Answers 4

Reset to default 5

Define them in your array as strings, or if it is ing from somewhere else:

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

It would be nice if there was the opposite of JSON_NUMERIC_CHECK but it doesn't look like there is.

Why can't you ensure the data is of the correct type in your php, before encoding it?

This might mean you have to cast it manually to strings...

json_decode() can convert large integers to strings, if you specify a flag in the function call:

$array = json_decode($json, true, 512, JSON_BIGINT_AS_STRING)

Because there isn't an opposite flag for JSON_NUMERIC_CHECK, I've created a function for that purpose.
It accepts one and multi dimensional arrays and there can be added for conditions to validate each element of te array.

function JSON_NUMERIC_STRING($array){
    foreach($array as $key => &$value){
        if(is_array($value)){
            $value = iterateMA($value);
        }elseif(is_numeric($value)){
            $value = strval($value);
        }
        // add more conditions if needed...
    }
    return $array;
}

$array = JSON_NUMERIC_STRING($array);

本文标签: javascriptForce jsonencode to create strings of numeric values in a flat arrayStack Overflow