php 7.0 vs 5.6 - array key to object difference -
question
why code produce different results when run in php5.6 vs php7.0?
background
i have following code:
<?php $assoc_array = [     'a' => 1,     'b' => 2,     'c' => 3,     'd' => 4 ]; $index_array = ['a','b','c','d']; $object = new \stdclass;  foreach($index_array $item) {     $object->$assoc_array[$item] = ""; }  print_r($object); when run in ubuntu 17.4, apache 2.4.25, php 7.0, this:
notice: array string conversion in /var/www/html/file.php on line 12 notice: array string conversion in /var/www/html/file.php on line 12 notice: array string conversion in /var/www/html/file.php on line 12 notice: array string conversion in /var/www/html/file.php on line 12  stdclass object (      [array] => array (          [a] =>          [b] =>          [c] =>          [d] =>      )  ) when run in same environment, switch php 5.6, this:
stdclass object (     [1] =>     [2] =>     [3] =>     [4] => ) it's not breaking code, got hung on why different , didn't start research.
nb. it's object i'm interested in here, not notices - error reporting on in both.
you can't in php7:   $object->$assoc_array[$item] = ""; see, notice, replace code one: 
<?php $assoc_array = [     'a' => 1,     'b' => 2,     'c' => 3,     'd' => 4 ]; $index_array = ['a','b','c','d']; $object = new \stdclass;  foreach($index_array $item) {     $object->{$assoc_array[$item]} = ""; }  print_r($object); you can find more informations here: https://github.com/tpunt/php7-reference#uniform-variable-syntax
Comments
Post a Comment