8
{"general":{
 "round-corner":"0",
 "border-stroke":"2",
 "background-color":"#ffffff"
 }
}

I have this json string, I know that php variable names doesn't support dashes. So what to do in this case ?

Xsmael
  • 3,624
  • 7
  • 44
  • 60
  • 1
    What about just changing them prior to decoding the string? str_replace() http://www.php.net/manual/en/function.str-replace.php – jtheman Jun 25 '14 at 13:56
  • Those will be rendered as keys in an array or properties on an object. Are you also using extract on that array? – mishu Jun 25 '14 at 13:56

2 Answers2

27

When dealing with valid json you don't need to do anything special to use the result in php as long as you don't use extract().

Admiditly it looks cleaner to let json_decode return an array here as Jay Bhatt suggests but you are also free to use a normal object as return (which is an instance of stdclass).

The properties of the returned object can be nearly anything. You just need to use the property name as a php-string instead of a hardcoded literal.

$obj->{'a sentence with spaces and umlauts äüö is valid here'}

<?php

$json = <<<JSON
{"general":{
 "round-corner":"0",
 "border-stroke":"2",
 "background-color äü??$%§":"#ffffff"
 }
}
JSON;

$obj = json_decode($json);

$keyName = "round-corner";
var_dump($obj->general->{'round-corner'});
var_dump($obj->general->$keyName);
var_dump($obj->general->{'background-color äü??$%§'});

Result

Community
  • 1
  • 1
Rangad
  • 2,110
  • 23
  • 29
  • Editors: Rangad deliberately included the extra characters in the background-color tag. Don't edit them out. – Kenster Jun 25 '14 at 17:33
  • what is the use of that "äü??$%§" ? – Xsmael Jun 25 '14 at 22:08
  • @Xsmael to exemplify that php does not limit you in any way when it comes to naming your code or importing naming conventions/options from another format (json here)(You may name your property ␀ (unicode: U+2400) if you want). See [here](http://stackoverflow.com/a/17973365/2912456) – Rangad Jun 25 '14 at 22:29
7

You can use an array format like this. Hyphened keys will work.

<?php

$json = '{"general":{
 "round-corner":"0",
 "border-stroke":"2",
 "background-color":"#ffffff"
 }
}';

$array = json_decode($json, true);

echo $array['general']['border-stroke']; // prints 2

?>

Here's a demo

Jay Bhatt
  • 5,601
  • 5
  • 40
  • 62
  • so if there another sub-key "type" to "background-color", ill do this to get it: `$array['general']['background-color']['type']` ? – Xsmael Jun 25 '14 at 14:34
  • thanks! one last thing, if there is just one key with a dash in the whole json object, will it cause json_decode() to return an array (like above)instead of an object? – Xsmael Jun 25 '14 at 16:00
  • @Xsmael If the variable names are invalid then the object might not be created. Since your using objects just as data containers it's better to use arrays in this case. If you really want to get objects then you'll have to remove hyphens from variable names. Will will add on to your performance. – Jay Bhatt Jun 25 '14 at 16:08
  • You do not need to use an array. Objects will just work with anything that is also a valid json-key-name. http://3v4l.org/YDG13 – Rangad Jun 25 '14 at 16:43
  • @Rangad Really?! so i can write $Obj->tiny-text ? – Xsmael Jun 25 '14 at 17:19
  • @JayBhatt but if i do that i will not be able to json_encode() it back for other process. – Xsmael Jun 25 '14 at 17:24