Page 1 of 1

Single associative array value grouping based on key

Posted: Fri Oct 27, 2017 12:43 pm
by azamat
https://www.testdome.com/for-developers ... tion/11840 I can`t complete this easy test :( .

I can`t figure out how to complete this one. I went so below so far.

<?php
class FileOwners
{
public static function groupByOwners($files)
{
$grouped = array();
foreach ($files as $value => $key){
$grouped[$key][$value] = $key;
//print "$key = $val\n";

}
return $grouped;

}
}

$files = array
(
"Input.txt" => "Randy",
"Code.py" => "Stan",
"Output.txt" => "Randy"
);
var_dump(FileOwners::groupByOwners($files));
?>

Can anyone show me how to do it? Thanks!

Re: Single associative array value grouping based on key

Posted: Sat Oct 28, 2017 2:12 am
by hyper
It would help if you explained what you are having problems with, don't provide links to problems which you have to solve on your own, we can help but will not do it for you.

What is the script doing, and what errors you are getting.

Post code using the </> code button.

And it should be:

Code: Select all

foreach ($myarray as $key => $value)