3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function getArrayFromHyphenArray($flatArray){ $deepArray = array(); foreach($flatArray as $key => $value){ $keysArray = explode('-', $key); $deepArray = recursiveSet($deepArray, $keysArray, $value); } } function recursiveSet(&$array, &$arg, $value){ static $count = 0; if(empty($arg)){ $array = $value; return $array; } $key = array_shift($arg); if(!isset($array[$key])){ print_r($key); $count++; echo $count; $array[$key] = array(); } return recursiveSet($array[$key],$arg,$value); } $flatArray = array('this-is-something' => 'value1', 'bif-is-else' => 'value3', 'this-different-else' => 'value2'); print_r(getArrayFromHyphenArray($flatArray));
based on DIUaT
Output for 7.0.0 - 7.2.0
this1is2something3bif4 Warning: Illegal string offset 'bif' in /in/v2iiH on line 21 Notice: Array to string conversion in /in/v2iiH on line 21 Warning: Illegal string offset 'bif' in /in/v2iiH on line 23 Fatal error: Uncaught Error: Only variables can be passed by reference in /in/v2iiH:23 Stack trace: #0 /in/v2iiH(6): recursiveSet('Aalue1', Array, 'value3') #1 /in/v2iiH(27): getArrayFromHyphenArray(Array) #2 {main} thrown in /in/v2iiH on line 23
Process exited with code 255.
Output for 5.4.11 - 5.6.28
this1is2something3bif4 Warning: Illegal string offset 'bif' in /in/v2iiH on line 21 Notice: Array to string conversion in /in/v2iiH on line 21 Warning: Illegal string offset 'bif' in /in/v2iiH on line 23 Fatal error: Only variables can be passed by reference in /in/v2iiH on line 23
Process exited with code 255.
Output for 5.3.21 - 5.3.29
this1is2something3 Fatal error: Only variables can be passed by reference in /in/v2iiH on line 23
Process exited with code 255.