3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $get_data = array( array( "MenuId" => "1", "MenuName" => "Perencanaan dan Pengadaan", "MenuParent" => "", "MenuLink" => "" ), array( "MenuId" => "1-1", "MenuName" => "RKA / DPA", "MenuParent" => "1", "MenuLink" => "" ), array( "MenuId" => "1-1-1", "MenuName" => "Daftar RKA / DPA", "MenuParent" => "1-1", "MenuLink" => "rkbu" ) ); function insert_into(&$array, array $keys, $value) { $last = array_pop($keys); foreach($keys as $key) { if(!array_key_exists($key, $array) || array_key_exists($key, $array) && !is_array($array[$key]['items'])) { $array[$key]['items'] = array(); } $array = &$array[$key]; } $array[$last] = $value; } $menuItems = array(); foreach($get_data as $item) { $levels = explode('-', $item['MenuId']); insert_into($menuItems, $levels, $item['MenuName']); } print_r($menuItems);
based on EQaJ6
Output for 7.0.0 - 7.2.6
Warning: Illegal string offset 'items' in /in/qfm5q on line 29 Warning: Illegal string offset 'items' in /in/qfm5q on line 30 Notice: Array to string conversion in /in/qfm5q on line 30 Warning: Illegal string offset 'items' in /in/qfm5q on line 29 Warning: Illegal string offset 'items' in /in/qfm5q on line 30 Notice: Array to string conversion in /in/qfm5q on line 30 Warning: array_key_exists() expects parameter 2 to be array, string given in /in/qfm5q on line 28 Fatal error: Uncaught Error: Cannot use string offset as an array in /in/qfm5q:30 Stack trace: #0 /in/qfm5q(41): insert_into('ARrencanaan dan...', Array, 'Daftar RKA / DP...') #1 {main} thrown in /in/qfm5q on line 30
Process exited with code 255.
Output for 5.4.0 - 5.6.28
Warning: Illegal string offset 'items' in /in/qfm5q on line 29 Warning: Illegal string offset 'items' in /in/qfm5q on line 30 Notice: Array to string conversion in /in/qfm5q on line 30 Warning: Illegal string offset 'items' in /in/qfm5q on line 29 Warning: Illegal string offset 'items' in /in/qfm5q on line 30 Notice: Array to string conversion in /in/qfm5q on line 30 Warning: array_key_exists() expects parameter 2 to be array, string given in /in/qfm5q on line 28 Fatal error: Cannot use string offset as an array in /in/qfm5q on line 30
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Notice: Array to string conversion in /in/qfm5q on line 30 Notice: Array to string conversion in /in/qfm5q on line 30 Warning: array_key_exists() expects parameter 2 to be array, string given in /in/qfm5q on line 28 Fatal error: Cannot use string offset as an array in /in/qfm5q on line 30
Process exited with code 255.
Output for 5.1.0 - 5.2.17
Notice: Array to string conversion in /in/qfm5q on line 30 Notice: Array to string conversion in /in/qfm5q on line 30 Warning: array_key_exists(): The second argument should be either an array or an object in /in/qfm5q on line 28 Fatal error: Cannot use string offset as an array in /in/qfm5q on line 30
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/qfm5q on line 24
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_ARRAY, expecting '&' or T_VARIABLE or T_CONST in /in/qfm5q on line 24
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE or T_CONST in /in/qfm5q on line 24
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'&'' or `T_VARIABLE' or `T_CONST' in /in/qfm5q on line 24
Process exited with code 255.