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

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

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

问题

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

array(4) {
  [0]=>
  array(8) {
    ["question_category"]=>
    string(11) "Engagement"
    [0]=>
    string(11) "Engagement"
    ["result"]=>
    string(6) "6.6667"
    [1]=>
    string(6) "6.6667"
    ["employee_first_name"]=>
    string(5) "Person A"
    [3]=>
    string(5) "Person A"
  }
  [1]=>
  array(8) {
    ["question_category"]=>
    string(9) "Tenure"
    [0]=>
    string(9) "Tenure"
    ["result"]=>
    string(6) "7.6667"
    [1]=>
    string(6) "7.6667"
    ["employee_first_name"]=>
    string(5) "Person A"
    [3]=>
    string(5) "Person A"
  }
  [2]=>
  array(8) {
    ["question_category"]=>
    string(11) "Engagement"
    [0]=>
    string(11) "Engagement"
    ["result"]=>
    string(6) "7.6667"
    [1]=>
    string(6) "7.6667"
    ["employee_first_name"]=>
    string(6) "Person B"
    [3]=>
    string(6) "Person B"
  }
  [3]=>
  array(8) {
    ["question_category"]=>
    string(9) "Tenure"
    [0]=>
    string(9) "Tenure"
    ["result"]=>
    string(6) "8.3333"
    [1]=>
    string(6) "8.3333"
    ["employee_first_name"]=>
    string(6) "Person B"
    [3]=>
    string(6) "Person B"
  }
}

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

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

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

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

而 'y' 是这个 'result':

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

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

英文:

I have a result from an sql query:

 array(4) {
[0]=>
array(8) {
["question_category"]=>
string(11) "Engagement"
[0]=>
string(11) "Engagement"
["result"]=>
string(6) "6.6667"
[1]=>
string(6) "6.6667"
["employee_first_name"]=>
string(5) "Person A"
[3]=>
string(5) "Person A"
}
[1]=>
array(8) {
["question_category"]=>
string(9) "Tenure"
[0]=>
string(9) "Tenure"
["result"]=>
string(6) "7.6667"
[1]=>
string(6) "7.6667"
["employee_first_name"]=>
string(5) "Person A"
[3]=>
string(5) "Person A"
}
[2]=>
array(8) {
["question_category"]=>
string(11) "Engagement"
[0]=>
string(11) "Engagement"
["result"]=>
string(6) "7.6667"
[1]=>
string(6) "7.6667"
["employee_first_name"]=>
string(6) "Person B"
[3]=>
string(6) "Person B"
}
[3]=>
array(8) {
["question_category"]=>
string(9) "Tenure"
[0]=>
string(9) "Tenure"
["result"]=>
string(6) "8.3333"
[1]=>
string(6) "8.3333"
["employee_first_name"]=>
string(6) "Person B"
[3]=>
string(6) "Person B"
}
}

And I want to end up with a results array:

$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.

$data = [
    [
        'question_category' => 'Engagement',
        'result' => '6.6667',
        'employee_first_name' => 'Person A',
    ],
    [
        'question_category' => 'Tenure',
        'result' => '7.6667',
        'employee_first_name' => 'Person A',
    ],
    [
        'question_category' => 'Engagement',
        'result' => '7.6667',
        'employee_first_name' => 'Person B',
    ],
    [
        'question_category' => 'Tenure',
        'result' => '8.3333',
        'employee_first_name' => 'Person B',
    ],
];

$grouped = [];
foreach($data as $row){
    // I think this is the mapping? If anything else exists,
    // this will throw
    $xOrY = match($row['question_category']){
        'Engagement' => 'x',
        'Tenure' => 'y',
    };
    
    // Group by name
    $grouped[$row['employee_first_name']][$xOrY] = $row['result'];
}

// Remove the names (optional)
$grouped = array_values($grouped);

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:

确定