3v4l.org

run code in 300+ PHP versions simultaneously
<?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', 'this-is-else' => 'value3', 'this-different-else' => 'value2'); print_r(getArrayFromHyphenArray($flatArray));
Output for 8.0.7 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
this1is2something3this4 Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /in/DIUaT:21 Stack trace: #0 /in/DIUaT(6): recursiveSet('value1', Array, 'value3') #1 /in/DIUaT(27): getArrayFromHyphenArray(Array) #2 {main} thrown in /in/DIUaT on line 21
Process exited with code 255.
Output for 8.0.0 - 8.0.6
this1is2something3this4 Warning: Array to string conversion in /in/DIUaT on line 21 Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /in/DIUaT:21 Stack trace: #0 /in/DIUaT(6): recursiveSet('value1', Array, 'value3') #1 /in/DIUaT(27): getArrayFromHyphenArray(Array) #2 {main} thrown in /in/DIUaT on line 21
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
this1is2something3this4 Warning: Illegal string offset 'this' in /in/DIUaT on line 21 Notice: Array to string conversion in /in/DIUaT on line 21 Warning: Illegal string offset 'this' in /in/DIUaT on line 23 Fatal error: Uncaught Error: Only variables can be passed by reference in /in/DIUaT:23 Stack trace: #0 /in/DIUaT(6): recursiveSet('Aalue1', Array, 'value3') #1 /in/DIUaT(27): getArrayFromHyphenArray(Array) #2 {main} thrown in /in/DIUaT on line 23
Process exited with code 255.
Output for 7.3.32 - 7.3.33
this1is2something3this4 Warning: Illegal string offset 'this' in /in/DIUaT on line 21 Warning: Illegal string offset 'this' in /in/DIUaT on line 23 Fatal error: Uncaught Error: Only variables can be passed by reference in /in/DIUaT:23 Stack trace: #0 /in/DIUaT(6): recursiveSet('Aalue1', Array, 'value3') #1 /in/DIUaT(27): getArrayFromHyphenArray(Array) #2 {main} thrown in /in/DIUaT on line 23
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
this1is2something3this4 Warning: Illegal string offset 'this' in /in/DIUaT on line 21 Notice: Array to string conversion in /in/DIUaT on line 21 Warning: Illegal string offset 'this' in /in/DIUaT on line 23 Fatal error: Only variables can be passed by reference in /in/DIUaT on line 23
Process exited with code 255.

preferences:
281.12 ms | 402 KiB | 326 Q