Question: 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
    }
]

```

Answer: 
You can simply typecast the `int` into `string`, like:

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

```

This will force `json_encode` to parse `1` as `"1"`. [Demo](https://eval.in/918270)

**Edit:** you can use the following function to typecast your multidimensional array([DEMO](https://eval.in/918441)):

```
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](https://eval.in/918447)):

```
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;
}

```
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"
}]

```