2

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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
janb
  • 360
  • 5
  • 16
  • Maybe this [link](http://stackoverflow.com/questions/1390983/php-json-encode-encoding-numbers-as-strings) can help. Regards ! – user12733 Oct 30 '13 at 15:47
  • @Tino that's the opposite of what I want, as mentioned – janb Oct 30 '13 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 Oct 30 '13 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. – CBroe Oct 30 '13 at 15:57
  • think there is no way. Only way is probably loop through you array and make it a string. http://stackoverflow.com/questions/1035634/converting-an-integer-to-a-string-in-php – uptownhr Oct 30 '13 at 15:57
  • @Spudley not really, as the library is displaying texts there (tooltips), but I only want to display the numbers as tooltips. – janb Oct 30 '13 at 15:58
  • @JamesLee (and CBroe) you are both right, I can enforce it that way. See my comment to dm03514's answer as to why I wanted to avoid just that – janb Oct 30 '13 at 16:00
  • easy) `parse_str(http_build_query($data), $r); echo json_encode($r);` – vp_arth Jul 03 '20 at 12:09

4 Answers4

5

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

dm03514
  • 54,664
  • 18
  • 108
  • 145
  • You are right, I can indeed do that. I was trying to avoid it as I wanted to use the same array twice, once with JSON_NUMERIC_CHECK (as the data array) and once enforced as strings (for the tooltips for said data). Which means I now have to add an extra loop and a new array. I would have preferred not to bloat my code like that. Ah well, it IS a solution. – janb Oct 30 '13 at 15:56
5

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

$data = json_encode(array_map('strval', $data));
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

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)
Shivanshu
  • 1,230
  • 1
  • 11
  • 17
  • not an option as I'm not decoding them at all. I build up a JSON string that is retrieved via $.getJSON() and then directly passed to the jQuery library – janb Oct 30 '13 at 16:09
0

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);
CIRCLE
  • 4,501
  • 5
  • 37
  • 56
  • Regardless of the fact that your recursive snippet doesn't declare `iterateMA()` anywhere, this process can be streamlined with `array_walk_recursive()`. Here is [my working equivalent to your intended script](https://stackoverflow.com/a/67023516/2943403). I also think it is inadvisable to name your custom function in allcaps which will confuse devs who would initially infer that syntax to mean a constant. – mickmackusa Apr 09 '21 at 16:09