Single associative array value grouping based on key

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
azamat
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Oct 27, 2017 12:30 pm

Fri Oct 27, 2017 12:43 pm

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!

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 693
Joined: Mon Feb 22, 2016 5:52 pm

Sat Oct 28, 2017 2:12 am

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)

Post Reply