3v4l.org

run code in 300+ PHP versions simultaneously
<?php if ( ! function_exists( 'str_ends_with' ) ) { function str_ends_with( $haystack, $needle ) { // Yes, I know, but I'm coding on mobile. forgive_me() return 0 === strpos( strrev( $haystack ), strrev( $needle ) ); } } $dirlist = array ( 'folder' => array ( 'name' => 'folder', 'perms' => 'urwxr-xr-x', 'permsn' => '0755', 'number' => false, 'owner' => false, 'group' => false, 'size' => 4096, 'lastmodunix' => 1673689008, 'lastmod' => 'Jan 14', 'time' => '09:36:48', 'type' => 'd', 'files' => array ( 'subfile.php' => array ( 'name' => 'subfile.php', 'perms' => 'urw-r--r--', 'permsn' => '0644', 'number' => false, 'owner' => false, 'group' => false, 'size' => 0, 'lastmodunix' => 1673689008, 'lastmod' => 'Jan 14', 'time' => '09:36:48', 'type' => 'f', ), 'subfolder' => array ( 'name' => 'subfolder', 'perms' => 'urwxr-xr-x', 'permsn' => '0755', 'number' => false, 'owner' => false, 'group' => false, 'size' => 4096, 'lastmodunix' => 1673689982, 'lastmod' => 'Jan 14', 'time' => '09:53:02', 'type' => 'd', 'files' => array ( 'subsubfile.php' => array ( 'name' => 'subsubfile.php', 'perms' => 'urw-r--r--', 'permsn' => '0644', 'number' => false, 'owner' => false, 'group' => false, 'size' => 0, 'lastmodunix' => 1673689982, 'lastmod' => 'Jan 14', 'time' => '09:53:02', 'type' => 'f', ), ), ), ), ), 'file.php' => array ( 'name' => 'file.php', 'perms' => 'urw-r--r--', 'permsn' => '0644', 'number' => false, 'owner' => false, 'group' => false, 'size' => 0, 'lastmodunix' => 1673689019, 'lastmod' => 'Jan 14', 'time' => '09:36:59', 'type' => 'f', ), ); $path = trailingslashit( '/root' ); echo headerme( 'Expected' ); echo $path, 'folder/subfile.php', PHP_EOL; echo $path, 'folder/subfolder/subsubfile.php', PHP_EOL; echo $path, 'file.php', PHP_EOL; echo headerme( 'array_walk_recursive()' ); array_walk_recursive( $dirlist, function ( $value, $key, $path ) { if ( str_ends_with( $value, '.php' ) ) { echo $path, $value, PHP_EOL; } }, $path ); echo headerme( 'map_deep()' ); map_deep( $dirlist, function( $value ) use ( $path ) { if ( str_ends_with( $value, '.php' ) ) { echo $path, $value, PHP_EOL; } } ); echo headerme( 'map_deep_with_index()' ); map_deep_with_index( $dirlist, function( $value, $key ) use ( $path ) { if ( str_ends_with( $value, '.php' ) ) { echo $path, $value, PHP_EOL; } } ); function map_deep( $value, $callback ) { if ( is_array( $value ) ) { foreach ( $value as $index => $item ) { $value[ $index ] = map_deep( $item, $callback ); } } elseif ( is_object( $value ) ) { $object_vars = get_object_vars( $value ); foreach ( $object_vars as $property_name => $property_value ) { $value->$property_name = map_deep( $property_value, $callback ); } } else { $value = call_user_func( $callback, $value ); } return $value; } function map_deep_with_index( $value, $callback, $index = null ) { if ( is_array( $value ) ) { foreach ( $value as $index => $item ) { $value[ $index ] = map_deep_with_index( $item, $callback, $index ); } } elseif ( is_object( $value ) ) { $object_vars = get_object_vars( $value ); foreach ( $object_vars as $property_name => $property_value ) { $value->$property_name = map_deep_with_index( $property_value, $callback ); } } else { $value = call_user_func( $callback, $value, $index ); } return $value; } function trailingslashit( $str ) { return rtrim( $str, '/' ) . '/'; } function headerme( $str ) { $equals = str_pad( '', mb_strlen( $str ), '=' ); return PHP_EOL . PHP_EOL . $equals . PHP_EOL . $str . PHP_EOL . $equals . PHP_EOL . PHP_EOL; }
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
======== Expected ======== /root/folder/subfile.php /root/folder/subfolder/subsubfile.php /root/file.php ====================== array_walk_recursive() ====================== /root/subfile.php /root/subsubfile.php /root/file.php ========== map_deep() ========== /root/subfile.php /root/subsubfile.php /root/file.php ===================== map_deep_with_index() ===================== /root/subfile.php /root/subsubfile.php /root/file.php

preferences:
119.07 ms | 403 KiB | 89 Q