0

很抱歉,因为我可能不会在这里使用正确的词汇,但我正在尝试找出“如何”修改以下代码,以便它创建数组数组(多维数组)。此代码创建下图中所示的结构,但我希望它创建数组数组(多维数组)。

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

在此处输入图像描述

任何帮助是极大的赞赏!托德

4

1 回答 1

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_filterarray_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

与之前相同的输出

沙盒

于 2019-04-03T04:18:37.820 回答