0

I build my Rest Apis on PHP 5.5.9, in which json_encode() was returning numeric values as a string, but now I am setting up same Rest Apis on different server with PHP 5.6 and here json_encode() is returning numeric values as numeric,

But I need the same behavior as PHP 5.5.9, because my client apps are way huge and I have been handling numeric as string value in all client Apps, so currently it is not possible for me to handle numeric as numeric, I need numeric as string, how can I achieve this?

For data

array("id" => 1);

On my original server which is based on 5.5.9, I was getting response like this:

{"id": "1"}

And now on another server which is based on 5.6, I am getting response like this:

{"id": 1}

But I need response in this form on my new server:

{"id": "1"}

Can anyone provide me a suitable solution for this?

EDIT

"I have multi-dimensional arrays and I am fetching data from database. I cannot modify all the Api Code"

Example response:

[   
    {
        "id": 4220,
        "user_id": 151,
        "truckId": 8,
        "truckType": "",
        "InsertionDateTime": "2017-05-30 08:25:20",
        "flag": 1
    }, 
    {
        "id": 4221,
        "user_id": 151,
        "truckId": 8,
        "truckType": "",
        "InsertionDateTime": "2017-05-30 08:34:28",
        "flag": 1
    }
]
4
  • 3
    have you tried array("id" => "1"); ? Commented Dec 13, 2017 at 10:02
  • 1
    I don't think a difference in PHP versions should make a difference here…!? 3v4l.org/Z79fl Commented Dec 13, 2017 at 10:05
  • 1
    Normally you need it the other way, I think your only option is to cast it to a string. echo json_encode(array("id" => (string) 1)); Commented Dec 13, 2017 at 10:05
  • 1
    @LawrenceCherone I have multi-dimensional arrays and I am fetching data from database. I cannot modify all the Api Code Commented Dec 13, 2017 at 10:13

3 Answers 3

2

You can simply typecast the int into string, like:

array("id" => (string)1);

This will force json_encode to parse 1 as "1". Demo

Edit: you can use the following function to typecast your multidimensional array(DEMO):

function intToString($array)
{
    $newArr = $array;
    foreach($array as $key => $value)
    {
        if(is_int($value))
        {
            $newArr[$key] = (string)$value;
        }
        elseif(is_array($value))
        {
            $newArr[$key] = intToString($value);
        }
    }
    return $newArr;
}

Edit 2: To deal with object of array too, try the following (DEMO):

function intToString($array, $type = 'array')
{
    $newArr = $array;
    foreach($array as $key => $value)
    {
        if($type == "array")
        {
            if(is_int($value))
            {
                $newArr[$key] = (string)$value;
            }
            elseif(is_array($value))
            {
                $newArr[$key] = intToString($value);
            }
            elseif(is_object($value))
            {
                $newArr[$key] = intToString($value, 'object');
            }
        }
        elseif($type == "object")
        {
            if(is_int($value))
            {
                $newArr->$key = (string)$value;
            }
            elseif(is_array($value))
            {
                $newArr->$key = intToString($value);
            }
            elseif(is_object($value))
            {
                $newArr->$key = intToString($value, 'object');
            }
        }
    }
    return $newArr;
}
Sign up to request clarification or add additional context in comments.

2 Comments

If you're going to make a somewhat generic function for multi-dimensional arrays, that function should be recursive and not fixed to a specific depth.
@deceze I believe it is recursive enough to match any depth now.
0

Apparently there was a proposal similar to what you're asking, and it was rejected: https://why-cant-we-have-nice-things.mwl.be/requests/json-numeric-as-string

So there is way to do this on the output end, as far as I know. You will have to do the casting on the input end, like others suggested. Iterate through your data set and cast the values. Unless you have billions of values, it shouldn't be a noticeable drain on your performance.

Comments

0

You can iterate to change in string as @mega6382 said with recursive manner

$jsonObj = json_decode($arr);
function changeToStr(&$obj){
  foreach($obj as &$elem){
    if(gettype($elem)!= "object"){
      $elem = (string)$elem;
    }else{
      changeToStr($elem);
    }
  } 
};
changeToStr($jsonObj);
echo json_encode($jsonObj);

Check live demo : https://eval.in/918430

OUTPUT :

[{
    "id": "4220",
    "user_id": "151",
    "truckId": "8",
    "truckType": "",
    "InsertionDateTime": "2017-05-30 08:25:20",
    "flag": "1"
}, {
    "id": "4221",
    "user_id": "151",
    "truckId": "8",
    "truckType": "",
    "InsertionDateTime": "2017-05-30 08:34:28",
    "flag": "1"
}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.