如何将每两个数组合并成一个数组并附加到新数组(在foreach循环之外)?

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

How can I merge every 2 arrays into 1 and append to new array (outside of loop) in foreach loop?

问题

我有一个来自SQL查询的结果:

  1. array(4) {
  2. [0]=>
  3. array(8) {
  4. ["question_category"]=>
  5. string(11) "Engagement"
  6. [0]=>
  7. string(11) "Engagement"
  8. ["result"]=>
  9. string(6) "6.6667"
  10. [1]=>
  11. string(6) "6.6667"
  12. ["employee_first_name"]=>
  13. string(5) "Person A"
  14. [3]=>
  15. string(5) "Person A"
  16. }
  17. [1]=>
  18. array(8) {
  19. ["question_category"]=>
  20. string(9) "Tenure"
  21. [0]=>
  22. string(9) "Tenure"
  23. ["result"]=>
  24. string(6) "7.6667"
  25. [1]=>
  26. string(6) "7.6667"
  27. ["employee_first_name"]=>
  28. string(5) "Person A"
  29. [3]=>
  30. string(5) "Person A"
  31. }
  32. [2]=>
  33. array(8) {
  34. ["question_category"]=>
  35. string(11) "Engagement"
  36. [0]=>
  37. string(11) "Engagement"
  38. ["result"]=>
  39. string(6) "7.6667"
  40. [1]=>
  41. string(6) "7.6667"
  42. ["employee_first_name"]=>
  43. string(6) "Person B"
  44. [3]=>
  45. string(6) "Person B"
  46. }
  47. [3]=>
  48. array(8) {
  49. ["question_category"]=>
  50. string(9) "Tenure"
  51. [0]=>
  52. string(9) "Tenure"
  53. ["result"]=>
  54. string(6) "8.3333"
  55. [1]=>
  56. string(6) "8.3333"
  57. ["employee_first_name"]=>
  58. string(6) "Person B"
  59. [3]=>
  60. string(6) "Person B"
  61. }
  62. }

我想要最终得到一个结果数组:

  1. $results = array(['x' => 6.6667, 'y' => 7.6667], ['x' => 7.6667, 'y' => 8.3333]);

其中 'x' 是这个 'result':

  1. string(11) "Engagement"
  2. ["result"]=>
  3. string(6) "6.6667"

而 'y' 是这个 'result':

  1. string(9) "Tenure"
  2. ["result"]=>
  3. string(6) "7.6667"

FYI... "result" 是从数据库 GROUP BY question_category 计算的 AVG('result')。我已经卡在这个问题上一段时间了,所以真的很感激一些意见!

英文:

I have a result from an sql query:

  1. array(4) {
  2. [0]=>
  3. array(8) {
  4. ["question_category"]=>
  5. string(11) "Engagement"
  6. [0]=>
  7. string(11) "Engagement"
  8. ["result"]=>
  9. string(6) "6.6667"
  10. [1]=>
  11. string(6) "6.6667"
  12. ["employee_first_name"]=>
  13. string(5) "Person A"
  14. [3]=>
  15. string(5) "Person A"
  16. }
  17. [1]=>
  18. array(8) {
  19. ["question_category"]=>
  20. string(9) "Tenure"
  21. [0]=>
  22. string(9) "Tenure"
  23. ["result"]=>
  24. string(6) "7.6667"
  25. [1]=>
  26. string(6) "7.6667"
  27. ["employee_first_name"]=>
  28. string(5) "Person A"
  29. [3]=>
  30. string(5) "Person A"
  31. }
  32. [2]=>
  33. array(8) {
  34. ["question_category"]=>
  35. string(11) "Engagement"
  36. [0]=>
  37. string(11) "Engagement"
  38. ["result"]=>
  39. string(6) "7.6667"
  40. [1]=>
  41. string(6) "7.6667"
  42. ["employee_first_name"]=>
  43. string(6) "Person B"
  44. [3]=>
  45. string(6) "Person B"
  46. }
  47. [3]=>
  48. array(8) {
  49. ["question_category"]=>
  50. string(9) "Tenure"
  51. [0]=>
  52. string(9) "Tenure"
  53. ["result"]=>
  54. string(6) "8.3333"
  55. [1]=>
  56. string(6) "8.3333"
  57. ["employee_first_name"]=>
  58. string(6) "Person B"
  59. [3]=>
  60. string(6) "Person B"
  61. }
  62. }

And I want to end up with a results array:

  1. $results = array(['x' => 6.6667, 'y' => 7.6667],['x' => 7.6667, 'y' => 8.3333]);

With 'x' being this 'result':

string(11) "Engagement"
["result"]=>
string(6) "6.6667"

and 'y' being this 'result':

string(9) "Tenure"
["result"]=>
string(6) "7.6667"

fyi... "result" is AVG('result') from DB GROUP BY question_category,...

I've been stuck on this for a while now, so I'd really appreciate some input!

答案1

得分: 0

正如注释中所指出的,我建议在SQL中解决这个问题。但如果出于某种原因你不能这样做,只需按员工姓名分组,根据需要将问题类别映射为x或y。

英文:

As noted in the comments, I'd recommend solving this in SQL. But if for whatever reason you can't, just group by employee name, and map the question category to x or y as needed.

  1. $data = [
  2. [
  3. 'question_category' => 'Engagement',
  4. 'result' => '6.6667',
  5. 'employee_first_name' => 'Person A',
  6. ],
  7. [
  8. 'question_category' => 'Tenure',
  9. 'result' => '7.6667',
  10. 'employee_first_name' => 'Person A',
  11. ],
  12. [
  13. 'question_category' => 'Engagement',
  14. 'result' => '7.6667',
  15. 'employee_first_name' => 'Person B',
  16. ],
  17. [
  18. 'question_category' => 'Tenure',
  19. 'result' => '8.3333',
  20. 'employee_first_name' => 'Person B',
  21. ],
  22. ];
  23. $grouped = [];
  24. foreach($data as $row){
  25. // I think this is the mapping? If anything else exists,
  26. // this will throw
  27. $xOrY = match($row['question_category']){
  28. 'Engagement' => 'x',
  29. 'Tenure' => 'y',
  30. };
  31. // Group by name
  32. $grouped[$row['employee_first_name']][$xOrY] = $row['result'];
  33. }
  34. // Remove the names (optional)
  35. $grouped = array_values($grouped);
  36. var_dump($grouped);

Demo: https://3v4l.org/PN1t4#v8.2.7

huangapple
  • 本文由 发表于 2023年6月16日 02:40:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484618.html
匿名

发表评论

匿名网友

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

确定