1
function groupByOwners(array $files) : array { return []; }

$files = array("Input.txt" => "Randy","Code.py" => "Stan","Output.txt" =>"Randy"); 

Print_r(groupByOwners($files);

My expected output is:

[Randy => [Input.txt, Output.txt] , Stan => [Code.py]]
0

1 Answer 1

2

You just need to iterate over your array, pushing each filename to a new array indexed by the names:

function groupByOwners(array $files) : array { 
    $output = array();
    foreach ($files as $file => $name) {
        $output[$name][] = $file;
    }
    return $output;
}

$files = array("Input.txt" => "Randy","Code.py" => "Stan","Output.txt" =>"Randy"); 

print_r(groupByOwners($files));

Output:

Array
(
    [Randy] => Array
        (
            [0] => Input.txt
            [1] => Output.txt
        )
    [Stan] => Array
        (
            [0] => Code.py
        )
)

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.