I have this somewhat embarrassingly simple problem with arrays I can't seem to figure out. I apologise for the title of this thread, it doesn't quite seem to cover what I want, but the code should clear up matters soon enough. Here goes:
Code: Select all
...
/* $id has been set to some integer value */
$result[$id] = array('foo' => $foo, 'bar' =>$bar, 'files' => array());
...
$files = $result[$id]['files'];
$handle = opendir('somedir');
while (($file = readdir($handle)) !== false)
$files[] = $file;
...
So basically I have this array $result containing another array in $result[$id]['files'], and I want to fill
that array with the names of the files of a given directory. In order to save on typing and to speed up processing I cache the array I want to fill in a variable $files, so naturally I would assume that when I start adding to this array, PHP would do The Right Thing and fill in $result[$id]['files']. This trick works in C, and it works in Javascript too. But the funny thing is: it doesn't with PHP. Instead it appears as if it fills up an array which is totally separate from the one in $result[$id]['files'].
My question: why doesn't the above work, and can I make it work without explicitly writing
in the while()-loop?