我正在尝试翻转数组,但它错过了同名键的值。我必须使用什么来向数组中多次出现的键添加几个值?
例如,对于
[
"Input.txt" => "Randy",
"Code.py" => "Stan",
"Output.txt" => "Randy"
]
groupByOwners 函数应该返回
[
"Randy" => ["Input.txt", "Output.txt"],
"Stan" => ["Code.py"]
]
当前代码:
class FileOwners
{
static $files;
public static function groupByOwners($files)
{
$flip = array_flip($files);
print_r($flip);
}
}
$files = array
(
"Input.txt" => "Randy",
"Code.py" => "Stan",
"Output.txt" => "Randy"
);
我的函数返回Array ( [Randy] => Output.txt [Stan] => Code.py ) NULL
。
因此缺少值“Input.txt”。这两个值必须是相同的键,那么如何将“Input.txt”和“Output.txt”放在键[Randy]的数组中?