Answers for "Implement a groupByOwners function that: Accepts an associative array"

1

“Implement a groupByOwners function that: Accepts an associative array” Code Answer

<?php
class FileOwners
{
    public static function groupByOwners($files)
    {
        $newArray = array();
        foreach($files as $key=>$f){
           	// this will return all the array value based on key
            $newArray[$f] = array_keys($files, $f);
            //$newArray[$f][] = $key;
        }
        return $newArray;
    }
}

$files = array
(
    "Input.txt" => "Randy",
    "Code.py" => "Stan",
    "Output.txt" => "Randy"
);
var_dump(FileOwners::groupByOwners($files));
Posted by: Guest on February-05-2021
0

Implement a groupByOwners function that: Accepts an associative array

function groupByOwners(array $files) : array
{
    $result = [];

    foreach($files as $file => $owner) {
        $result[$owner][] = $file;
    }

    return $result;
}
Posted by: Guest on October-23-2020

Code answers related to "Implement a groupByOwners function that: Accepts an associative array"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language