Answers for "collect file name with owner name in array"

PHP
1

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"]]
Posted by: Guest on February-04-2021

Browse Popular Code Answers by Language