很抱歉,因为我可能不会在这里使用正确的词汇,但是我试图弄清楚“如何”修改以下代码,以便它创建数组数组(多维数组) 。这段代码创建了下图所示的结构,但是我希望它创建数组数组(多维数组)。
基本上,我希望1001、1002、1004等成为主数组。嵌套数组将是其中包含#1001,#1002等的字符串。您会注意到字符串中的#与原始数组中的数字相对应。
$combinedAssignmentData = [];
foreach($assignmentsYES as $key=>$assignedIDs){
$levels = array($assignedIDs);
foreach($levels as $key=>$level){
echo "<strong>$level</strong><br>";
foreach($studentIDsubmissions as $k=>$individualSubmission){
if (strpos($individualSubmission, $level) !== false) {
echo "--$individualSubmission<br>";
}
}
}
}
var_export($ assignmentsYES);
array ( 0 => '1001', 1 => '1002', 2 => '1004', 3 => '1005', 4 => '1007', 5 => '1008', 6 => '1009', 7 => '1015', 8 => '1028', 9 => '1029', )
var_export($ 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', )
任何帮助将不胜感激! 托德
答案 0 :(得分:1)
你在这里
$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
那里有一个肤浅的循环。
与以前相同的输出