I'm trying to add an array to another array at key inside a foreach loop. Here is my function. Here is my function:
$dirs = scandir( $dir );
$results = array( 'folders' => [] );
foreach ( $dirs as $dir ) {
if ( is_dir( $this::uploads_path() . '/' . $dir ) ) {
array_push( $results['folders'], $dir );
} else {
$file = array(
'filename' => $dir,
'filesize' => 1000
);
$results['files'][] = $file;
}
}
return $results;
It seems however I try, I get keys with empty values. Like this:
'files' =>
array (size=7)
0 =>
array (size=2)
...
1 =>
array (size=2)
...
2 =>
array (size=2)
...
3 =>
array (size=2)
...
4 =>
array (size=2)
...
5 =>
array (size=2)
...
6 =>
array (size=2)
...
I've also tried delaying adding the array by building a new array with the file information and adding it to $results outside the foreach loop. Thanks for the help. Would love an explanation as to why I'm getting empty values rather than just a fixed code example.
filename
andfilesize
... but instead it prints...
for brevity. – BareNakedCoder Dec 4 '16 at 1:12var_dump($results['files'][0])
to confirm what's really there. – BareNakedCoder Dec 4 '16 at 1:17