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){ if(empty($arg)){ $array = $value; return $array; } $key = array_shift($arg); if(!isset($array[$key])){ var_dump($key); $array[$key] = array(); } return recursiveSet($array[$key],$arg,$value); } $arra = array('this-is-some' => 'value', 'this-is-second' => 'value'); print_r(getArrayFromHyphenArray($arra));
based on gkT1T
Output for 7.0.0 - 7.2.0
string(4) "this" string(2) "is" string(4) "some" string(4) "this" Warning: Illegal string offset 'this' in /in/JppQn on line 17 Notice: Array to string conversion in /in/JppQn on line 17 Warning: Illegal string offset 'this' in /in/JppQn on line 19 Fatal error: Uncaught Error: Only variables can be passed by reference in /in/JppQn:19 Stack trace: #0 /in/JppQn(6): recursiveSet('Aalue', Array, 'value') #1 /in/JppQn(23): getArrayFromHyphenArray(Array) #2 {main} thrown in /in/JppQn on line 19
Process exited with code 255.
Output for 5.4.11 - 5.6.28
string(4) "this" string(2) "is" string(4) "some" string(4) "this" Warning: Illegal string offset 'this' in /in/JppQn on line 17 Notice: Array to string conversion in /in/JppQn on line 17 Warning: Illegal string offset 'this' in /in/JppQn on line 19 Fatal error: Only variables can be passed by reference in /in/JppQn on line 19
Process exited with code 255.
Output for 5.3.21 - 5.3.29
string(4) "this" string(2) "is" string(4) "some" Fatal error: Only variables can be passed by reference in /in/JppQn on line 19
Process exited with code 255.