Question: Let's say I have such JSON:

```
{
 id: 1,
 somevalue: "text"
}

```

and I want to create this JSON by PHP function [json\_encode](http://php.net/manual/en/function.json-encode.php). I can pretty easy get this JSON in form:

```
{
 "id": "1",
 "somevalue": "text"
}

```

or, using `JSON_NUMERIC_CHECK`, in form where "id" will be numeric, but "somevalue" can be either numeric or text, depend on its content.

How can I make JSON where "somevalue" always be in text format (with quotes). I'll parse it by other language, where it is important.

Answer: 
To make `somevalue` always in "text" format:

```
$somevalue1 = 1;
$somevalue2 = "text";

$json1 = array("id" => 1, "somevalue" => (string) $somevalue1);
$json2 = array("id" => 1, "somevalue" => (string) $somevalue2);

echo json_encode($json1); // outputs {"id":1,"somevalue":"1"}
echo json_encode($json2); // outputs {"id":1,"somevalue":"text"}

```
On PHP> 5.3.3., you can use json\_decode($array, JSON\_NUMERIC\_CHECK);

If you do not have PHP 5.3.3 or more I wrote this recursive function:

```
function json_encode_with_numbers($array) {
    if(is_array($array)) {
        if(count($array)>0 && array_keys($array) !== range(0, count($array) - 1)) {
            echo '{'; 

            $isFirst = true;
            foreach($array as $key=>$item) {
                if(!$isFirst) {
                    echo ",";
                }
                echo '"'.$key.'":';
                json_encode_with_numbers($item);
                $isFirst = false;
            }
            echo '}';
        } else {
            echo '['; 
            $isFirst = true;
            foreach($array as $item) {
                if(!$isFirst) {
                    echo ",";
                }
                json_encode_with_numbers($item);
                $isFirst = false;
            }
            echo ']';
        }
    } else {
        if(is_numeric($array)) {
            echo $array;
        } elseif ($array == null) {
            echo "null";
        } else {
            echo '"'.str_replace(array('"', '\\'), array('\"', '\\\\'), $array).'"'; // escape special chars
        }

    }

}

```