JSON Object element met hyphen

Accessing elements within an object that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

Bron: PHP: json_decode - Manual

Voorbeeld:

$json = '{"foo-bar": 12345}';
$obj = json_decode($json);

Fout:

echo $obj->foo-bar;

Goed:

echo $obj->{'foo-bar'};

Meer informatie: Hyphen

Categorieën
PHP
Zoeken