DEV Community

AmitGupta
AmitGupta

Posted on

How to implement a function with array

 echo "<pre>";
function groupByOwners(array $files) :array
{
    $result=array();
    foreach($files as $key=>$value)
    {
        $result[$value][]=$key;
    }
    return $result; 
} 
 $files = array
   (
    "Input.txt" => "Randy",
    "Code.py" => "Stan",
    "Output.txt" => "Randy"
   );
Enter fullscreen mode Exit fullscreen mode

var_dump(groupByOwners($files));
//output
array(2) {
["Randy"]=>
array(2) {
[0]=>
string(9) "Input.txt"
[1]=>
string(10) "Output.txt"
}
["Stan"]=>
array(1) {
[0]=>
string(7) "Code.py"
}
}

Top comments (0)