php - Adding a new key value pair to a JSON object where key has a random ID -


this question has answer here:

i know how add new key value pair json through php like:

$json->newobject = "value"; 

what can't figure out how give key of pair, random id.

i've tried like:

$id = rand(99, 9999); $json["newobject" . $id] = "value"; 

with error of: fatal error: cannot use object of type stdclass array in /home/methodjs/public_html/projects/chat/send.php on line 8

and:

$id = rand(99, 9999); $json->("newobject" . $id) = "value"; 

with error of: parse error: syntax error, unexpected '(', expecting t_string or t_variable or '{' or '$' in /home/methodjs/public_html/projects/chat/send.php on line 8

there must simple way this, hope. help.

sorry dublicate

this want:

$json->{"newobject".$id} = "value"; 

the feature called variable properties. lets assign , properties object in php using strings , variables.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -