need help understanding php object property names versus array keys
<p>I'm having some trouble using PHP object property names in a flexible way, similar to array keys.</p>

<p>My understanding is that an array key can be any string or integer, e.g</p>

<pre><code>$arr = array("crash-me" =&gt; "value");
</code></pre>

<p>is a valid key/value pair.</p>

<p>Now if I "cast" that to an object:</p>

<pre><code>$obj = (object) $arr;
</code></pre>

<p>have I done something illegal, because of the dash in the key string?</p>

<p>A simple example shows my dilema:</p>

<pre><code>&lt;?php
error_reporting(-1);
$obj = new stdClass;
$prop = "crash-me";
$arr = array();
$obj-&gt;$prop = "no crash"; 
$obj-&gt;$prop = $arr; // bp here and $obj-&gt;$prop is correct
print_r($obj); // bp here, inspect $obj-&gt;$prop and... xdebug/php will crash.
exit;
</code></pre>

<p>This will crash eclipse-pdt using xdebug when you inspect the value of the $obj before the print_r().</p>

<p>However, without bp, contents are printed correctly from print_r() at exit.</p>

<pre><code>stdClass Object ( [crash-me] =&gt; Array ( ) ) 
</code></pre>

<p>Note that if I stuff something into the array when assigning it to the property, things are OK:</p>

<pre><code>$obj = new stdClass;
$prop = "crash-me";
$arr = array("works_ok");
$obj-&gt;$prop = "no crash";
$obj-&gt;$prop = $arr;
print_r($obj);
exit;
</code></pre>

<p>Bottom-line, I'd like to use property names with dashes and possibly other punctuation similar to array key. Is it possible? Or am I doing something illegal by assigning an empty array, in which case wouldn't an error be caught?</p>

<p>Thanks ahead for your kind consideration!</p>

<p>on edit, I forgot another part of puzzle - the code below does not crash. In this case, the property name does not have a dash, however, the array key is empty!</p>

<pre><code>$obj = new stdClass;
$prop = "crash_me";  // note underscore
$arr = array();
$obj-&gt;$prop = "no crash"; 
$obj-&gt;$prop = $arr; // bp here and $obj-&gt;$prop is correct
print_r($obj); // bp here, inspect $obj-&gt;$prop and... no crash!
exit;
</code></pre>

<p>Sorry for the confusion! It's probably something obvious I am missing.</p>

<p>On second edit-</p>

<p>To clarify, I was hoping to read property names from json_decode() and simply use the objects without having to convert to arrays. It was an experiment with dynamic code generation using keys from json data files. Per discussion below, looks like if I want to continue with PHP for this, I'll need the $arr['key1']['key2']... syntax and have the decoder use the "array" flag.</p>

<p>Thanks for the great replies!</p>
