PHP如何将嵌套的foreach变成数组的数组(多维数组)

PHP How To Turn Nested Foreach Into An Array of Arrays (Multidimensional Array)

抱歉,因为我可能不会在这里使用正确的词汇,但我正在尝试弄清楚 "how-to" 修改以下代码,以便它创建数组的数组(多维大批)。此代码创建下图所示的结构,但我希望它创建数组数组(多维数组)。

基本上,我希望 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', )

非常感谢任何帮助! 托德

给你

$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
        (
        )

)

Sandbox

*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(....)); 中以重置它们。数组过滤器保留原始数组中的键。在大多数情况下,密钥并不重要,所以我不会担心,除非你真的需要。

更新

想了想发了这个

be better to use a regular expression

我们为什么不选择这个:

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

与之前相同的输出

Sandbox