干得好
$assignmentsYES = array ( 0 => '1001', 1 => '1002', 2 => '1004', 3 => '1005', 4 => '1007', 5 => '1008', 6 => '1009', 7 => '1015', 8 => '1028', 9 => '1029', );
$studentIDsubmissions = array ( 0 => '346623@guhsd.net|TD-Share Test #1001|NO', 1 => '346623@guhsd.net|TD-Share Test #1001|NO', 2 => '346623@guhsd.net|TD-Share Test #1001|NO', 3 => '346623@guhsd.net|TD-Share Test #1001|NO', 4 => '346623@guhsd.net|TD-No Excuse Reflection #1002|YES', 5 => '346623@guhsd.net|TD-No Excuse Reflection #1002|YES', 6 => '346623@guhsd.net|TD-No Excuse Reflection #1002|YES', 7 => '346623@guhsd.net|TD-About Me #1004|YES', 8 => '346623@guhsd.net|TD-Calendar #1007|YES', 9 => '346623@guhsd.net|TD-Wage Tracker #1008|YES', 10 => '346623@guhsd.net|TD-Stock Portfolio #1009|YES', 11 => '346623@guhsd.net|TD-Collaboration #1005|YES', 12 => '346623@guhsd.net|TD-Stock Portfolio #1009|YES', 13 => '346623@guhsd.net|TD-Collaboration #1005|YES', 14 => '346623@guhsd.net|TD-Dream Vacation Presentation #1015|YES', );
$combinedAssignmentData = [];
foreach($assignmentsYES as $key=>>$level){
$combinedAssignmentData[$level] =
array_filter(
$studentIDsubmissions,
function($item)use($level){
return strpos($item, '#'.$level) !== false;
}
);
}
print_r($combinedAssignmentData);
输出
Array
(
[1001] => Array
(
[0] => 346623@guhsd.net|TD-Share Test #1001|NO
[1] => 346623@guhsd.net|TD-Share Test #1001|NO
[2] => 346623@guhsd.net|TD-Share Test #1001|NO
[3] => 346623@guhsd.net|TD-Share Test #1001|NO
)
[1002] => Array
(
[4] => 346623@guhsd.net|TD-No Excuse Reflection #1002|YES
[5] => 346623@guhsd.net|TD-No Excuse Reflection #1002|YES
[6] => 346623@guhsd.net|TD-No Excuse Reflection #1002|YES
)
[1004] => Array
(
[7] => 346623@guhsd.net|TD-About Me #1004|YES
)
[1005] => Array
(
[11] => 346623@guhsd.net|TD-Collaboration #1005|YES
[13] => 346623@guhsd.net|TD-Collaboration #1005|YES
)
[1007] => Array
(
[8] => 346623@guhsd.net|TD-Calendar #1007|YES
)
[1008] => Array
(
[9] => 346623@guhsd.net|TD-Wage Tracker #1008|YES
)
[1009] => Array
(
[10] => 346623@guhsd.net|TD-Stock Portfolio #1009|YES
[12] => 346623@guhsd.net|TD-Stock Portfolio #1009|YES
)
[1015] => Array
(
[14] => 346623@guhsd.net|TD-Dream Vacation Presentation #1015|YES
)
[1028] => Array
(
)
[1029] => Array
(
)
)
沙盒
*PS
我#
在这里添加了一个strpos($item, '#'.$level)
,这将提高一点准确性。最好使用正则表达式(在数组过滤器回调中)
function($item)use($level){
return preg_match('/#'.$level.'\|/', $item); //match `#{id}|`
}
考虑例如匹配1001
到 id 10012
~ strpos 将只匹配1001
部分而不考虑。
如果子数组的奇数键出现错误,您可以将其包裹起来array_filter
以array_values(array_filter(....));
重置它们。数组过滤器保留原始数组中的键。在大多数情况下,密钥并不重要,所以除非你真的需要,否则我不会担心。
更新
想了想发了这个
最好使用正则表达式
我们为什么不选择这个:
$assignmentsYES = array ( 0 => '1001', 1 => '1002', 2 => '1004', 3 => '1005', 4 => '1007', 5 => '1008', 6 => '1009', 7 => '1015', 8 => '1028', 9 => '1029', );
$studentIDsubmissions = array ( 0 => '346623@guhsd.net|TD-Share Test #1001|NO', 1 => '346623@guhsd.net|TD-Share Test #1001|NO', 2 => '346623@guhsd.net|TD-Share Test #1001|NO', 3 => '346623@guhsd.net|TD-Share Test #1001|NO', 4 => '346623@guhsd.net|TD-No Excuse Reflection #1002|YES', 5 => '346623@guhsd.net|TD-No Excuse Reflection #1002|YES', 6 => '346623@guhsd.net|TD-No Excuse Reflection #1002|YES', 7 => '346623@guhsd.net|TD-About Me #1004|YES', 8 => '346623@guhsd.net|TD-Calendar #1007|YES', 9 => '346623@guhsd.net|TD-Wage Tracker #1008|YES', 10 => '346623@guhsd.net|TD-Stock Portfolio #1009|YES', 11 => '346623@guhsd.net|TD-Collaboration #1005|YES', 12 => '346623@guhsd.net|TD-Stock Portfolio #1009|YES', 13 => '346623@guhsd.net|TD-Collaboration #1005|YES', 14 => '346623@guhsd.net|TD-Dream Vacation Presentation #1015|YES', );
$combinedAssignmentData = [];
foreach($assignmentsYES as $key=>$level){
$combinedAssignmentData[$level] = preg_grep('/#'.$level.'\|/', $studentIDsubmissions);
}
print_r($combinedAssignmentData);
使用 Preg Grep 比使用数组过滤器和带有正则表达式的回调要干净一些。我也意识到你在那里$levels = array($assignedIDs);
或基本上$levels = array($level);
或只是有一个肤浅的循环$level
。
与之前相同的输出
沙盒