如何在 Laravel 或 PHP 中将自定义键应用于我的顺序数组/数组集合?

huangapple go评论114阅读模式
英文:

How to apply custom keys to my sequential array/collection of arrays using Laravel or PHP?

问题

我正在尝试在Laravel中对集合应用自定义键。

我找到的最佳方法是使用transformmapWithKeys等函数。

  1. $result = $collection->map(function ($item) {
  2. return [
  3. 'custom_key_name_1' => $item[0],
  4. 'custom_key_name_2' => $item[1],
  5. 'custom_key_name_3' => $item[2],
  6. ];
  7. });

我希望能找到类似以下的方法:

  1. $keys = ['custom_key_name_1', 'custom_key_name_2', 'custom_key_name_3'];
  2. $result = $collection->withKeys($keys);

您是否知道还有其他方式可以用更简洁的代码来实现这个目标?

我正在使用一个旧的Laravel 5.5项目。

输入示例

  1. $collection = collect([
  2. [ 'first', 'second', 'third' ],
  3. [ '1', '2', '3' ],
  4. [ '1st', '2nd', '3rd' ],
  5. ]);

预期输出

  1. [
  2. [
  3. 'custom_key_name_1' => 'first',
  4. 'custom_key_name_2' => 'second',
  5. 'custom_key_name_3' => 'third',
  6. ],
  7. [
  8. 'custom_key_name_1' => '1',
  9. 'custom_key_name_2' => '2',
  10. 'custom_key_name_3' => '3',
  11. ],
  12. [
  13. 'custom_key_name_1' => '1st',
  14. 'custom_key_name_2' => '2nd',
  15. 'custom_key_name_3' => '3rd',
  16. ],
  17. ]
英文:

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.

  1. $result = $collection->map(function ($item) {
  2. return [
  3. 'custom_key_name_1' => $item[0],
  4. 'custom_key_name_2' => $item[1],
  5. 'custom_key_name_3' => $item[2],
  6. ];
  7. });

I was hoping to find something like:

  1. $keys = ['custom_key_name_1', 'custom_key_name_2', 'custom_key_name_3'];
  2. 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

  1. $collection = collect([
  2. [ 'first', 'second', 'third' ],
  3. [ '1', '2', '3' ],
  4. [ '1st', '2nd', '3rd' ],
  5. ]);

Expected Output

  1. [
  2. [
  3. 'custom_key_name_1' => 'first',
  4. 'custom_key_name_2' => 'second',
  5. 'custom_key_name_3' => 'third',
  6. ],
  7. [
  8. 'custom_key_name_1' => '1',
  9. 'custom_key_name_2' => '2',
  10. 'custom_key_name_3' => '3',
  11. ],
  12. [
  13. 'custom_key_name_1' => '1st',
  14. 'custom_key_name_2' => '2nd',
  15. 'custom_key_name_3' => '3rd',
  16. ],
  17. ]

答案1

得分: 1

由于您正在使用一个数组集合,您可以使用PHP的array_combine()方法将键和值组合在一起:

  1. $collection = collect([
  2. [ 'first', 'second', 'third' ],
  3. [ '1', '2', '3' ],
  4. [ '1st', '2nd', '3rd' ],
  5. ]);
  6. $keys = collect(['custom_key_name_1', 'custom_key_name_2', 'custom_key_name_3']);
  7. $result = $collection->map(fn ($v) => $keys->combine($v));
  8. dump($result);

结果:

  1. Illuminate\Support\Collection^ {#5189
  2. #items: array:3 [
  3. 0 => array:3 [
  4. "custom_key_name_1" => "first"
  5. "custom_key_name_2" => "second"
  6. "custom_key_name_3" => "third"
  7. ]
  8. 1 => array:3 [
  9. "custom_key_name_1" => "1"
  10. "custom_key_name_2" => "2"
  11. "custom_key_name_3" => "3"
  12. ]
  13. 2 => array:3 [
  14. "custom_key_name_1" => "1st"
  15. "custom_key_name_2" => "2nd"
  16. "custom_key_name_3" => "3rd"
  17. ]
  18. ]
  19. #escapeWhenCastingToString: false
  20. }

需要注意的重要事项是,根据文档,如果键和值的元素数量不匹配,将会引发"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:

  1. $collection = collect([
  2. [ 'first', 'second', 'third' ],
  3. [ '1', '2', '3' ],
  4. [ '1st', '2nd', '3rd' ],
  5. ]);
  6. $keys = collect(['custom_key_name_1', 'custom_key_name_2', 'custom_key_name_3']);
  7. $result = $collection->map(fn ($v) => $keys->combine($v));
  8. dump($result);

Result:

  1. Illuminate\Support\Collection^ {#5189
  2. #items: array:3 [
  3. 0 => array:3 [
  4. "custom_key_name_1" => "first"
  5. "custom_key_name_2" => "second"
  6. "custom_key_name_3" => "third"
  7. ]
  8. 1 => array:3 [
  9. "custom_key_name_1" => "1"
  10. "custom_key_name_2" => "2"
  11. "custom_key_name_3" => "3"
  12. ]
  13. 2 => array:3 [
  14. "custom_key_name_1" => "1st"
  15. "custom_key_name_2" => "2nd"
  16. "custom_key_name_3" => "3rd"
  17. ]
  18. ]
  19. #escapeWhenCastingToString: false
  20. }

The important caveat being that, per the documentation, "a ValueError is thrown if the number of elements in keys and values does not match."

huangapple
  • 本文由 发表于 2023年2月18日 07:22:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定