请让我尝试根据课程重新组合数组。例如,检查所有数组中是否存在“基本经济学”,然后返回与“基本经济学”匹配的所有数组元素的结果,而不是0,1,2等。
student => [
0 => [
"name" => "John Doe",
"gender" => "Male",
"department" => "Economics",
"courses" => [
"Elementary Economics",
"Principle of Economics",
"Macro Economics"
"Money Market"
]
],
1 => [
"name" => "John Doe",
"gender" => "Male",
"department" => "Economics",
"courses" => [
"Advanced Economics",
"Principle of Economics",
"Micro Economics"
"Money Market"
]
]
2 => [
"name" => "John Doe",
"gender" => "Male",
"department" => "Economics",
"courses" => [
"Elementary Economics",
"Principle of Economics",
"Macro Economics"
"Value Market"
]
]
]我想按课程分组,我试过使用array_filter,它没有按照我的意愿工作,或者我做错了。
$result = array_filter($student, function ($item) use ($student['courses']) {
return \in_array($item, $student['courses'], true);
}, \ARRAY_FILTER_USE_KEY);发布于 2022-05-19 12:25:06
我通过安装和使用array-group-by解决了我的问题
$ composer require mcaskill/php-array-group-by$grouped = array_group_by( $student, "course" );https://stackoverflow.com/questions/72292402
复制相似问题