开发者

Help with getting first value of array in PHP

Stumped by this one:

I have a function that returns an array of folders in开发者_JAVA技巧 a given directory. When I iterate through the array, I can see all the items. However, if I try to print the first item, I get nothing:

function get_folders($dir) {
    return array_filter(scandir($dir), function ($item) use ($dir) {
        return (is_dir($dir.'/'.$item) && $item != "." && $item != "..") ;
    });
}
$folders = get_folders(".");
$first_folder = $folders[0]; 
echo $first_folder; // returns blank.

Interestingly, if I don't filter out the "." and "..", then $first_folder does print ".". Can anyone explain this?


As noted in the manual: Array keys are preserved when using array_filter().

The keys are preserved from the call to scandir() which will include the . and .. directories, meaning your filtered array will start at 2 or more (whatever the key is for the first directory).

A simple fix would be to wrap the whole thing in array_values() to get the resultant array having keys starting from 0 and incrementing by one.

return array_values(array_filter(...));

If you don't actually care about the keys, then basic array functions could be of use like key or current.


From the array_filter manual:

Iterates over each value in the input array passing them to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.

So, scandir($dir) gets you an array with . at key 0, thus if you later filter . out, there will be nothing at key 0 in the result.

If you want to know what the first key of the resulting array is, try the array_keys function.

EDIT: actually, salathe's suggestion to use array_values is better in your case than getting the keys from array_keys.


can you print the folders and see if there is some key you can get the first folder with as opposed to selecting the index of zero? It seems lke $folders[0] would work unless the entries are stored with a different set of indexes / keys.

Try doing a

print_r($folders) 

to see if it outputs your first folder and all other folders as expected with the corresponding indexes you're looking for.


array_filter preserves array keys, so if the first two elements have been removed, then the "new first" element has an index of 2. To convert that into an array indexed starting from 0, use array_values:

return array_values(array_filter(scandir($dir), function ($item) use ($dir) {
    ...


As others have said, array_filter preserves the keys in the array. Given that initially the item '.' has key 0, the filtered array ends up not having any item with key 0 (you can verify this with print_r($folders). So this line:

$first_folder = $folders[0];

ends up generating an E_NOTICE saying that there is no key 0 in the array (having notices turned on would alert you to the cause of the problem immediately, I highly recommend it) and giving $first_folder the value null.

To get the first value of the filtered array, regardless of key, use reset:

$first_folder = reset($folders); 
if($first_folder === false) {
    echo 'No folders!';
}
else {
    echo 'First folder: '.$first_folder;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜