Skip to content

Instantly share code, notes, and snippets.

@JamesHusband
Created August 30, 2018 15:01
Show Gist options
  • Save JamesHusband/d260df0f378e290711c34933c1080667 to your computer and use it in GitHub Desktop.
Save JamesHusband/d260df0f378e290711c34933c1080667 to your computer and use it in GitHub Desktop.
<?php
/*
Accepts an associative array containing the file owner name for each file name.
Returns an associative array containing an array of file names for each owner name, in any order.
For example, for associative array ["Input.txt" => "Randy", "Code.py" => "Stan", "Output.txt" => "Randy"]
the groupByOwners function should return ["Randy" => ["Input.txt", "Output.txt"], "Stan" => ["Code.py"]].
*/
class FileOwners {
public static function group_by_owners( $files ) {
$owners = array();
foreach ( $files as $key => $value ) {
$owners[ $value ][] = $key;
}
return $owners;
}
}
$files = array(
'Input.txt' => 'Randy',
'Code.py' => 'Stan',
'Output.txt' => 'Randy',
);
var_dump( FileOwners::group_by_owners( $files ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment