title: Why does PHP 7 add an array to stdClass, if the name come from an array?
body: <p>I have a code snippet (not mine), that is acting different in PHP 5.6 and PHP 7.</p>
  
  <p>Code:</p>
  
  <pre><code>$obj-&gt;order = new stdClass;
  $obj-&gt;address = new stdClass;
  foreach($item as $key=&gt;$value){
          $k = explode(".", $key);
          if(...) {
              if($k[0] == 'order') $obj-&gt;order-&gt;$k[1] = $value;
              elseif ($k[0] == 'address') $obj-&gt;address-&gt;$k[1] = $value;
          }
  }
  </code></pre>
  
  <p>There are a few values for $key:</p>
  
  <ul>
  <li>order.week</li>
  <li>order.weekfrom</li>
  <li>...</li>
  <li>order.params</li>
  </ul>
  
  <p>(PHP 7) print_r prints:</p>
  
  <pre><code>Array
  (
      [0] =&gt; stdClass Object
          (
              [order] =&gt; stdClass Object
                  (
                      [Array] =&gt; Array
                          (
                              [1] =&gt; json
                          )
                  )
          )
  )
  </code></pre>
  
  <p>(PHP 5.6) print_r prints:</p>
  
  <pre><code>Array
  (
      [0] =&gt; stdClass Object
          (
              [order] =&gt; stdClass Object
                  (
                      [week] =&gt; ...
                      [weekfrom] =&gt; ...
                      ...
                      [params] =&gt; json
                  )
          )
  )
  </code></pre>
  
  <p>I have to change the Code to get the same results:</p>
  
  <pre><code>$obj-&gt;order = new stdClass;
  $obj-&gt;address = new stdClass;
  foreach($item as $key=&gt;$value){
          $k = explode(".", $key);
          $foo = $k[1]; // insert var and add k[1] to it
          if(...) {
              if($k[0] == 'order') $obj-&gt;order-&gt;$foo = $value;
              elseif ($k[0] == 'address') $obj-&gt;address-&gt;$foo = $value;
          }
  }
  </code></pre>
  
  <p>What happens imo is, that PHP 7 always overides the value added to the stdclass with </p>
  
  <blockquote>
    <p>$obj->order->$k[1]</p>
  </blockquote>
  
  <p>If I use </p>
  
  <blockquote>
    <p>$obj->order->$foo </p>
  </blockquote>
  
  <p>the values are appended.</p>
  
  <p>Could somebody explain what happens here? I will gladly provide more details if needed.</p>