Dawid Góra
Dawid Góra

Reputation: 165

JSON_FORCE_OBJECT in lower version of PHP

My PHP server doesn't accept second parameter for json_encode (and I need JSON_FORCE_OBJECT). Is there any way to achive simmilar effect as JSON_FORCE_OBJECT if server doesn't support it? Mybe you know some custom functions simmilar to json_encode?

Upvotes: 0

Views: 939

Answers (1)

Markus
Markus

Reputation: 440

This should work, too:

$data = array(
    'foo' => (object) array('bar' => 'foo'),
);
echo json_encode($data); // should output {"foo": {"bar": "foo"}}

Upvotes: 2

Related Questions