英文:
How to apply custom keys to my sequential array/collection of arrays using Laravel or PHP?
问题
我正在尝试在Laravel中对集合应用自定义键。
我找到的最佳方法是使用transform
或mapWithKeys
等函数。
$result = $collection->map(function ($item) {
return [
'custom_key_name_1' => $item[0],
'custom_key_name_2' => $item[1],
'custom_key_name_3' => $item[2],
];
});
我希望能找到类似以下的方法:
$keys = ['custom_key_name_1', 'custom_key_name_2', 'custom_key_name_3'];
$result = $collection->withKeys($keys);
您是否知道还有其他方式可以用更简洁的代码来实现这个目标?
我正在使用一个旧的Laravel 5.5项目。
输入示例
$collection = collect([
[ 'first', 'second', 'third' ],
[ '1', '2', '3' ],
[ '1st', '2nd', '3rd' ],
]);
预期输出
[
[
'custom_key_name_1' => 'first',
'custom_key_name_2' => 'second',
'custom_key_name_3' => 'third',
],
[
'custom_key_name_1' => '1',
'custom_key_name_2' => '2',
'custom_key_name_3' => '3',
],
[
'custom_key_name_1' => '1st',
'custom_key_name_2' => '2nd',
'custom_key_name_3' => '3rd',
],
]
英文:
I'm trying to apply custom keys to a collection in laravel.
The best way that I found was using functions like transform
or mapWithKeys
.
$result = $collection->map(function ($item) {
return [
'custom_key_name_1' => $item[0],
'custom_key_name_2' => $item[1],
'custom_key_name_3' => $item[2],
];
});
I was hoping to find something like:
$keys = ['custom_key_name_1', 'custom_key_name_2', 'custom_key_name_3'];
result = $collection->withKeys($keys);
Do you know any other way to do that with a less verbose code?
I'm working in a legacy Laravel 5.5 project.
Input Example
$collection = collect([
[ 'first', 'second', 'third' ],
[ '1', '2', '3' ],
[ '1st', '2nd', '3rd' ],
]);
Expected Output
[
[
'custom_key_name_1' => 'first',
'custom_key_name_2' => 'second',
'custom_key_name_3' => 'third',
],
[
'custom_key_name_1' => '1',
'custom_key_name_2' => '2',
'custom_key_name_3' => '3',
],
[
'custom_key_name_1' => '1st',
'custom_key_name_2' => '2nd',
'custom_key_name_3' => '3rd',
],
]
答案1
得分: 1
由于您正在使用一个数组集合,您可以使用PHP的array_combine()
方法将键和值组合在一起:
$collection = collect([
[ 'first', 'second', 'third' ],
[ '1', '2', '3' ],
[ '1st', '2nd', '3rd' ],
]);
$keys = collect(['custom_key_name_1', 'custom_key_name_2', 'custom_key_name_3']);
$result = $collection->map(fn ($v) => $keys->combine($v));
dump($result);
结果:
Illuminate\Support\Collection^ {#5189
#items: array:3 [
0 => array:3 [
"custom_key_name_1" => "first"
"custom_key_name_2" => "second"
"custom_key_name_3" => "third"
]
1 => array:3 [
"custom_key_name_1" => "1"
"custom_key_name_2" => "2"
"custom_key_name_3" => "3"
]
2 => array:3 [
"custom_key_name_1" => "1st"
"custom_key_name_2" => "2nd"
"custom_key_name_3" => "3rd"
]
]
#escapeWhenCastingToString: false
}
需要注意的重要事项是,根据文档,如果键和值的元素数量不匹配,将会引发"ValueError"错误。
英文:
Since you're working with a collection of arrays, you can use PHP's array_combine()
method to put the keys and values together:
$collection = collect([
[ 'first', 'second', 'third' ],
[ '1', '2', '3' ],
[ '1st', '2nd', '3rd' ],
]);
$keys = collect(['custom_key_name_1', 'custom_key_name_2', 'custom_key_name_3']);
$result = $collection->map(fn ($v) => $keys->combine($v));
dump($result);
Result:
Illuminate\Support\Collection^ {#5189
#items: array:3 [
0 => array:3 [
"custom_key_name_1" => "first"
"custom_key_name_2" => "second"
"custom_key_name_3" => "third"
]
1 => array:3 [
"custom_key_name_1" => "1"
"custom_key_name_2" => "2"
"custom_key_name_3" => "3"
]
2 => array:3 [
"custom_key_name_1" => "1st"
"custom_key_name_2" => "2nd"
"custom_key_name_3" => "3rd"
]
]
#escapeWhenCastingToString: false
}
The important caveat being that, per the documentation, "a ValueError is thrown if the number of elements in keys and values does not match."
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论