object.php
Table of Contents
Functions
- object() : string
- Generate a JavaScript-like object string.
Functions
object()
Generate a JavaScript-like object string.
object([array<string|int, mixed>|string|null $keyValues = [] ][, bool $useSpace = false ]) : string
This function converts input into a string representing a JavaScript object,
e.g., {name:'Eka',age:48}
. Supported input types:
- Array of key-value pairs:
[ ['key1','value1'], ['key2','value2'] ]
- Associative array:
[ 'key1' => 'value1', 'key2' => 'value2' ]
- Preformatted string:
"key1:'value1',key2:'value2'"
- Null → returns empty braces
}
.
Each array element can be:
- a sub-array with exactly two elements
[key, value]
, - an associative key-value pair,
- or a string already formatted as
key:value
.
Optionally, spaces can be added around braces and after commas with $useSpace
.
Parameters
- $keyValues : array<string|int, mixed>|string|null = []
-
$keyValues Array of pairs, associative array, string, or null
- $useSpace : bool = false
-
Add spaces around braces and after commas
Tags
Return values
string —JavaScript-like object string