Skip to content

Instantly share code, notes, and snippets.

@campusboy87
Created May 31, 2017 10:24
Show Gist options
  • Save campusboy87/1f481de1d138535623b55f1575682d22 to your computer and use it in GitHub Desktop.
Save campusboy87/1f481de1d138535623b55f1575682d22 to your computer and use it in GitHub Desktop.
<?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