collect file name with owner name in array code example

Example: collect file name with owner name in array

<?php
function groupByOwners(array $files) : array
{
	$newArray = array();
    foreach($files as $key=>$f){
    	//print " $key => $f <br/>";
        //$newArray[$f] = array_keys($files, $f);
        $newArray[$f][] = $key; #stackOverflow Answer
    }
    return $newArray;
}

$files = array
(
    "Input.txt" => "Randy",
    "Code.py" => "Stan",
    "Output.txt" => "Randy"
);
//var_dump(groupByOwners($files));
print_r(groupByOwners($files));
?>
Given the Array:
["Input.txt" => "Randy", "Code.py" => "Stan", "Output.txt" => "Randy"]
Expected Returns:
["Randy" => ["Input.txt", "Output.txt"], "Stan" => ["Code.py"]]

Tags:

Php Example