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: 
Make sure values you want to be non string (like int or boolean) are entered as such into your source array:

```
<?php
$a = array('id' => 1, 'really' => true, 'somevalue' => 'text');
var_dump( json_decode( json_encode( $a ) ) );

```

gives expected:

```
object(stdClass)#1 (2) {
  ["id"]=>
  int(1)
  ["really"]=>
  bool(true)
  ["somevalue"]=>
  string(4) "text"
}

```

**EDIT**

If you want it always to be strings, then put strings in your array in first place:

```
<?php
$a = array('id' => '1', 'really' => 'true', 'somevalue' => 'text');
var_dump( json_decode( json_encode( $a ) ) );

```

would give

```
object(stdClass)#1 (3) {
  ["id"]=>
  string(1) "1"
  ["really"]=>
  string(4) "true"
  ["somevalue"]=>
  string(4) "text"
}

```

but that kills the whole purpose of having different variable types.

Of you can convert your array prior json encoding:

```
<?php
$a = array('id' => 1, 'really' => true, 'somevalue' => 'text');

$tmp = array();
foreach( $a as $key=>$val ) {
   $tmp[$key] = (string)$val;
}

var_dump( json_decode( json_encode( $tmp ) ) );

```

would give in the end:

```
object(stdClass)#1 (3) {
  ["id"]=>
  string(1) "1"
  ["really"]=>
  string(1) "1"
  ["somevalue"]=>
  string(4) "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
        }

    }

}

```