使用 array_flip 函数后同一键缺少值
Missing values from the same key after using array_flip function
我正在尝试翻转数组,但它遗漏了同名键的值。
我必须使用什么来向数组中多次出现的键添加几个值?
例如,对于
[
"Input.txt" => "Randy",
"Code.py" => "Stan",
"Output.txt" => "Randy"
]
groupByOwners 函数应该 return
[
"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"
);
我的函数returnArray ( [Randy] => Output.txt [Stan] => Code.py ) NULL
.
因此缺少值 "Input.txt"。对于两个值,它必须是相同的键,所以我如何将 "Input.txt" 和 "Output.txt" 放入键 [Randy] 的数组中?
一个有点快速但有点老套的解决方案:
php > $files = array
php > (
php ( "Input.txt" => "Randy",
php ( "Code.py" => "Stan",
php ( "Output.txt" => "Randy"
php ( );
php > var_dump(array_reduce(array_keys($files), function($p, $c) use (&$files) { $p[$files[$c]] = $p[$files[$c]] ?? []; $p[$files[$c]][] = $c; return $p; }, []));
array(2) {
["Randy"]=>
array(2) {
[0]=>
string(9) "Input.txt"
[1]=>
string(10) "Output.txt"
}
["Stan"]=>
array(1) {
[0]=>
string(7) "Code.py"
}
}
注意:“??”的使用需要 PHP 7.0.
只是为了将重要部分从一行中拉出来,并使其至少稍微更具可读性:
array_reduce(array_keys($files), function($p, $c) uses (&$files) {
$p[$files[$c]] = $p[$files[$c]] ?? [];
$p[$files[$c]][] = $c;
}, []);
您可以轻松地使用 if(isset(...)) 逻辑来确保 $p 中的数组元素存在。
你必须自己循环并构建一个新数组:
$files = array(
"Input.txt" => "Randy",
"Code.py" => "Stan",
"Output.txt" => "Randy"
);
$new_files = array();
foreach($files as $k=>$v)
{
$new_files[$v][] = $k;
}
print_r($new_files);
我正在尝试翻转数组,但它遗漏了同名键的值。 我必须使用什么来向数组中多次出现的键添加几个值?
例如,对于
[
"Input.txt" => "Randy",
"Code.py" => "Stan",
"Output.txt" => "Randy"
]
groupByOwners 函数应该 return
[
"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"
);
我的函数returnArray ( [Randy] => Output.txt [Stan] => Code.py ) NULL
.
因此缺少值 "Input.txt"。对于两个值,它必须是相同的键,所以我如何将 "Input.txt" 和 "Output.txt" 放入键 [Randy] 的数组中?
一个有点快速但有点老套的解决方案:
php > $files = array
php > (
php ( "Input.txt" => "Randy",
php ( "Code.py" => "Stan",
php ( "Output.txt" => "Randy"
php ( );
php > var_dump(array_reduce(array_keys($files), function($p, $c) use (&$files) { $p[$files[$c]] = $p[$files[$c]] ?? []; $p[$files[$c]][] = $c; return $p; }, []));
array(2) {
["Randy"]=>
array(2) {
[0]=>
string(9) "Input.txt"
[1]=>
string(10) "Output.txt"
}
["Stan"]=>
array(1) {
[0]=>
string(7) "Code.py"
}
}
注意:“??”的使用需要 PHP 7.0.
只是为了将重要部分从一行中拉出来,并使其至少稍微更具可读性:
array_reduce(array_keys($files), function($p, $c) uses (&$files) {
$p[$files[$c]] = $p[$files[$c]] ?? [];
$p[$files[$c]][] = $c;
}, []);
您可以轻松地使用 if(isset(...)) 逻辑来确保 $p 中的数组元素存在。
你必须自己循环并构建一个新数组:
$files = array(
"Input.txt" => "Randy",
"Code.py" => "Stan",
"Output.txt" => "Randy"
);
$new_files = array();
foreach($files as $k=>$v)
{
$new_files[$v][] = $k;
}
print_r($new_files);