Created
May 31, 2017 10:24
-
-
Save campusboy87/1f481de1d138535623b55f1575682d22 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Function | |
* | |
* @param [string|null] $dir Directory | |
* @return mixed | |
*/ | |
function dir_to_array( $dir = null ) { | |
$result = array(); | |
$cdir = scandir( $dir ); | |
foreach ( $cdir as $key => $value ) { | |
if ( ! in_array( $value, array( '.', '..' ), true ) ) { | |
if ( is_dir( $dir . DIRECTORY_SEPARATOR . $value ) ) { | |
$result[ $value ] = dir_to_array( $dir . DIRECTORY_SEPARATOR . $value ); | |
} else { | |
$result[] = "{$dir}/$value"; | |
} | |
} | |
} | |
if ( ! is_array( $result ) ) { | |
return array(); | |
} | |
ksort( $result ); | |
return $result; | |
} | |
/** | |
* Array flatten | |
* | |
* @param array $array Array | |
* | |
* @return array | |
*/ | |
function array_flatten( $array ) { | |
$return = array(); | |
foreach ( $array as $key => $value ) { | |
if ( is_array( $value ) ) { | |
$return = array_merge( $return, array_flatten( $value ) ); | |
} else { | |
$return[ $key ] = $value; | |
} | |
} | |
return $return; | |
} | |
var_dump( array_flatten( dir_to_array( 'includes' ) ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment