3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = json_decode('{"1":{"id":"1","source_name":"kk56ca1d0f2378f","company_id":"1","lft":"1","rgt":"18","parent_id":"0","children":{"2":{"id":"2","source_name":"kk56ca1d17f3f63","company_id":"1","lft":"2","rgt":"3","parent_id":"1","children":[]},"3":{"id":"3","source_name":"kk56ca1d1ebe975","company_id":"1","lft":"4","rgt":"13","parent_id":"1","children":{"6":{"id":"6","source_name":"kk56ca1fc882ac0","company_id":"1","lft":"5","rgt":"10","parent_id":"3","children":[]}}}}}}',1); function convert_array( $array ) { $retval = array(); foreach( $array as $row ) { $child = array(); $child['id'] = $row['id']; $child['text'] = $row['source_name']; $child['parent_id'] = $row['parent_id']; if( count( $row['children'] ) ) { $child['nodes'] = convert_array( $row['children'] ); } else { $child['nodes'] = array(); } $retval[] = $child; } return $retval; } $result = convert_array( $array ); print_r( $result );
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
Array ( [0] => Array ( [id] => 1 [text] => kk56ca1d0f2378f [parent_id] => 0 [nodes] => Array ( [0] => Array ( [id] => 2 [text] => kk56ca1d17f3f63 [parent_id] => 1 [nodes] => Array ( ) ) [1] => Array ( [id] => 3 [text] => kk56ca1d1ebe975 [parent_id] => 1 [nodes] => Array ( [0] => Array ( [id] => 6 [text] => kk56ca1fc882ac0 [parent_id] => 3 [nodes] => Array ( ) ) ) ) ) ) )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
217.74 ms | 408 KiB | 5 Q