0
    $showcaseObject = new stdClass();
    $generalObject = new stdClass();
    $generalObject->roundCorner = 0;
    $generalObject->borderStroke = 2;
    $generalObject->backgroundColor = '#fff';

    $showcaseObject->general = $generalObject;

    echo json_encode($showcaseObject);

and i get something like this

{"general":{
 "roundCorner":"0",
 "borderStroke":"2",
 "backgroundColor":"#ffffff"
 }
}

Now i want to get something like this

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

i try to change above code like under code, and get syntax error, unexpected '='

 $generalObject->round-corner = $row->general_round_corner_radius;
 $generalObject->border-stroke = $row->general_border_stroke;
 $generalObject->background-color = $row->background_color;

Help me !

2
  • Dash / minus is not legal for use in PHP variable (or property, class, function, etc.) names. us2.php.net/manual/en/language.variables.basics.php Commented Aug 14, 2010 at 2:01
  • thank ! i try with $generalObject->{'round-corner'} and it work Commented Aug 14, 2010 at 3:56

3 Answers 3

1

You could use the brace syntax when defining the object members:

$showcaseObject = new stdClass();
$generalObject = new stdClass();
$generalObject->{'round-corner'} = 0;
$generalObject->{'border-stroke'} = 2;
$generalObject->{'background-color'} = '#fff';

$showcaseObject->general = $generalObject;

echo json_encode($showcaseObject);

This brace syntax allows you to use expressions rather than just identifiers.

Sign up to request clarification or add additional context in comments.

Comments

0

To convert first form to second, traverse over each key and convert the keyname.

foreach ($generalObject as $keyName => $keyValue) {
    $newKey = strtolower(preg_replace('/([^A-Z])([A-Z])/', "$1_$2", $keyName));
    $generalObject[$newKey] = $keyValue;
    unset($generalObject[$keyName]);
}

To access them (this is what you're having a problem with), use bracket notation:

$generalObject['hyphenated-name']

1 Comment

thank for your anwser. But how i can to use $generalObject[$newKey] = $keyValue.The variable $generalObject is instance of stdClass
0

I think the easiest way may be to simply do some string replacement on the JSON strings that you have, to get the hyphen characters in where they couldn't be automatically inserted due to php's allow variable charset:

$showcaseObject = new stdClass();
$generalObject = new stdClass();
$generalObject->roundCorner = 0;
$generalObject->borderStroke = 2;
$generalObject->backgroundColor = '#fff';

$showcaseObject->general = $generalObject;

$jsonStr = json_encode($showcaseObject);
$jsonStr = str_replace('"roundCorner":', '"round-corner":', $jsonStr);
$jsonStr = str_replace('"borderStroke":', '"border-stroke":', $jsonStr);
$jsonStr = str_replace('"backgroundColor":', '"background-color":', $jsonStr);

echo $jsonStr;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.