gpt4 book ai didi

php - 将具有相同标识符的数组中的 ROWS 分组

转载 作者:行者123 更新时间:2023-12-02 05:04:36 27 4
gpt4 key购买 nike

如何在 foreach 循环内运行下面的这个数组,并将共享相同 section_id 的行组合在一起?

我用注释 block 标记了所有行和部分。

这是数组

Array ( 
[0] => Array ( // Row 1
[assessment_selection_id] => 63
[assessment_id] => 32
[section_id] => 1 // Section 1
[question_id] => 1
[selection] => 2
[timestamp] => 1368160586
)
[1] => Array ( // Row 2
[assessment_selection_id] => 61
[assessment_id] => 32
[section_id] => 2 // Section 2
[question_id] => 1
[selection] => 3
[timestamp] => 1368160510
)
[2] => Array ( // Row 3
[assessment_selection_id] => 61
[assessment_id] => 32
[section_id] => 2 // Section 2
[question_id] => 1
[selection] => 3
[timestamp] => 1368160510
)

[3] => Array ( // Row 4
[assessment_selection_id] => 61
[assessment_id] => 32
[section_id] => 2 // Section 2
[question_id] => 1
[selection] => 3
[timestamp] => 1368160510
)
)


预期结果

Array ( 
[0] => Array ( // Section 1
[0] => Array ( // Row 1
[assessment_selection_id] => 63
[assessment_id] => 32
[section_id] => 1
[question_id] => 1
[selection] => 2
[timestamp] => 1368160586
)
)
[1] => Array ( // Section 2
[0] => Array ( // Row 1
[assessment_selection_id] => 61
[assessment_id] => 32
[section_id] => 2
[question_id] => 1
[selection] => 2
[timestamp] => 1368160586
)
[1] => Array ( // Row 2
[assessment_selection_id] => 61
[assessment_id] => 32
[section_id] => 2
[question_id] => 1
[selection] => 2
[timestamp] => 1368160586
)
[2] => Array ( // Row 3
[assessment_selection_id] => 61
[assessment_id] => 32
[section_id] => 2
[question_id] => 1
[selection] => 2
[timestamp] => 1368160586
)
)
)


没有数组的预期结果

第 1 部分

  • 第 1 行

    assessment_selection_id,评估_id,section_id,问题编号,选择,时间戳

第 2 部分

  • 第 1 行

    assessment_selection_id,评估_id,section_id,问题编号,选择,时间戳

  • 第 2 行

    assessment_selection_id,评估_id,section_id,问题编号,选择,时间戳

  • 第 3 行

    assessment_selection_id,评估_id,section_id,问题编号,选择,时间戳

最佳答案

假设您将数组保存在 $myArray 中这样做:

$newArray=array();
foreach($myArray as $val){
$newKey=$val['section_id'];
$newArray[$newKey][]=$val
}
print_r($newArray);

关于php - 将具有相同标识符的数组中的 ROWS 分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16475897/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com