So long as your system isn't confined by open_basedir or something like that, this should work. Personally, I need this to get the names of the groups by their numeric ID without having to use a bunch of calls to filegroup() or by using DirectoryIterator::getGroup, although now that I take a closer look at the latter it seems like a better and better idea. Originally, I was going to use stat() and parse the results from what my script finds in /etc/group.
I might still use this for something or other, or perhaps not. Either way, here it is for your using and viewing pleasure.
The script will read the contents of /etc/group and return an array where the keys are the user group names and the value is their numeric id. That's just how I whipped it up though it would probably make more sense to have it the other way around.
<?php
$g = file_get_contents('/etc/group');
$g = explode("\n", $g);
$c = count($g) - 1;
$groups = array();
$x = 0;
foreach ($g as $row) {
if ($x === $c) {break;}
$row = explode(':', $row);
$groups[$row[0]] = intval($row[2]);
$x++;
}
echo json_encode($groups);
?>