I can't answer my own question because it is a duplicate of this one so here goes:
Below is my basic solution to this problem. Simply prepend 'function:' to a string for the string to not be wrapped in quotes. Feel free to amend as you wish!
From this array:

I got this result:
{"showPreviewOnForm":1,"onclose":plform.updateViewports,"uploadScript":"/lib/ajaxstream/upload.php","uploadTo":"temp","more":{"elem":1,"btdev":2,"0":"cdc","BBC":["name","orange",3,5]}}
As you can see, it works!
/**
* Stringify an array into something that JavaScript understands. Typing 'function:' before a string causes that string to not
* be encased in quotes, meaning that JS can understand that it is a function
* @param array $input The array to turn into a JS object
* @return string A JSON-like string
*/
function json_stringify($input) {
$outtext = '';
$opening = '{';
$closing = '}';
$inner = array();
$numericarray = array_keys($input) === range(0, count($input) - 1);
if ($numericarray) {
// This is a numerically sequential array
$opening = '[';
$closing = ']';
}
foreach ($input as $key => $val) {
if (is_string($val) && preg_match("/^function\:/", $val)) {
// The value is a string and begins with 'function:'. Do not encase it in quotes
$val = substr($val, 9);
} else if (is_int($val)) {
// The value is an integer
$val = (int) $val;
} else if (is_float($val)) {
// The value is a float
$val = (float) $val;
} else if (!is_bool($val)) {
// Keep booleans as they are, and for everything else, come here
$val = is_array($val) ? self::json_stringify($val) : "\"$val\"";
}
$inner[] = ($numericarray ? '' : "\"$key\":") . $val;
}
$outtext .= implode(',',$inner);
return "$opening$outtext$closing";
}
Note: One should only use this function if you need the string to be interpreted as an object straight away. It should not be used if you intend to use JSON.parse
with it