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); var_dump($keysArray); $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 JppQn
Output for 7.0.0 - 7.2.0
array(3) { [0]=> string(4) "this" [1]=> string(2) "is" [2]=> string(4) "some" } string(4) "this" string(2) "is" string(4) "some" array(3) { [0]=> string(4) "this" [1]=> string(2) "is" [2]=> string(6) "second" } string(4) "this" Warning: Illegal string offset 'this' in /in/tq8YY on line 18 Notice: Array to string conversion in /in/tq8YY on line 18 Warning: Illegal string offset 'this' in /in/tq8YY on line 20 Fatal error: Uncaught Error: Only variables can be passed by reference in /in/tq8YY:20 Stack trace: #0 /in/tq8YY(7): recursiveSet('Aalue', Array, 'value') #1 /in/tq8YY(24): getArrayFromHyphenArray(Array) #2 {main} thrown in /in/tq8YY on line 20
Process exited with code 255.
Output for 5.4.11 - 5.6.28
array(3) { [0]=> string(4) "this" [1]=> string(2) "is" [2]=> string(4) "some" } string(4) "this" string(2) "is" string(4) "some" array(3) { [0]=> string(4) "this" [1]=> string(2) "is" [2]=> string(6) "second" } string(4) "this" Warning: Illegal string offset 'this' in /in/tq8YY on line 18 Notice: Array to string conversion in /in/tq8YY on line 18 Warning: Illegal string offset 'this' in /in/tq8YY on line 20 Fatal error: Only variables can be passed by reference in /in/tq8YY on line 20
Process exited with code 255.
Output for 5.3.21 - 5.3.29
array(3) { [0]=> string(4) "this" [1]=> string(2) "is" [2]=> string(4) "some" } string(4) "this" string(2) "is" string(4) "some" array(3) { [0]=> string(4) "this" [1]=> string(2) "is" [2]=> string(6) "second" } Fatal error: Only variables can be passed by reference in /in/tq8YY on line 20
Process exited with code 255.