如何对For-each循环值进行分组并根据分组值执行

时间:2016-12-16 20:02:19

标签: php arrays foreach

对不起但是我是新手,所以经过很多时间我没有得到正确的解决方案。

我想将具有相同值的数组分组。 例如,数组的总大小为3,如下所示,它将为每个循环执行3次但我只想执行循环2次,因为它只包含2个唯一的class_id。

所以根据我作为输出我只想执行2次,因为它包含2个唯一的class_id

array(3) {
  [0]=>
  array(2) {
    ["subject_id"]=>
    string(1) "4"
    ["class_id"]=>
    string(1) "1"
  }
  [1]=>
  array(2) {
    ["subject_id"]=>
    string(1) "5"
    ["class_id"]=>
    string(1) "1"
  }
  [2]=>
  array(2) {
    ["subject_id"]=>
    string(1) "7"
    ["class_id"]=>
    string(1) "2"
  }
}

PHP代码

function view_class()
{
    global $conn;
    global $email;
    global $sub_cls;
    global $school_id;
    foreach($sub_cls as $index => $record)
    {
        var_dump($sub_cls);
        $class_id = $record['class_id'];
        $subject_id = $record['subject_id'];
        //echo "<script>alert($class_id)</script>";
        $run = mysqli_query($conn,"select * from class where class_id = ".$class_id." group by $class_id");
        while($row = mysqli_fetch_array($run))
        {
            $class_id = $row['class_id'];
            $class_name = $row['class_name'];
            echo "<option value='$class_id'>$class_name</option>";
        }
    }
}

2 个答案:

答案 0 :(得分:0)

好的,所以最简单的方法是将一个新的值数组合并在一起。

// Your original array
$lessons = [
    [
        'subject_id' => "4",
        'class_id' => "1",
    ],
    [
        'subject_id' => "5",
        'class_id' => "1",
    ],
    [
        'subject_id' => "7",
        'class_id' => "2",
    ],
];

// The new array
$merged = [];

// Loop through original array
foreach ($lessons as $lesson) {

    // Store some values for easier usage
    $classId = $lesson['class_id'];
    $subjectId = $lesson['subject_id'];

    // If class not in the merged set, initialize it
    if (! isset($merged[$classId])) {
        $merged[$classId] = [
            'class_id' => $classId,
            'subject_id' => [],
        ];
    }

    // Add subject in array under the class
    $merged[$classId]['subject_id'][] = $subjectId;

}

输出结果为:

array (size=2)
  1 => 
    array (size=2)
      'class_id' => string '1' (length=1)
      'subject_id' => 
        array (size=2)
          0 => string '4' (length=1)
          1 => string '5' (length=1)
  2 => 
    array (size=2)
      'class_id' => string '2' (length=1)
      'subject_id' => 
        array (size=1)
          0 => string '7' (length=1)

答案 1 :(得分:0)

使用另一个数组来跟踪您已处理的类ID。

$classes_processed = array();
foreach ($sub_cls as $index => $record) {
    $class_id = $record['class_id'];
    if (isset($classes_processed[$class_id]) {
        continue;
    }
    $classes_processed[$class_id] = true;
    $subject_id = $record['subject_id'];
    $run = mysqli_query($conn,"select class_name from class where class_id = ".$class_id." limit 1");
    $row = mysqli_fetch_assoc($run);
    $class_name = $row['class_name'];
    echo "<option value='$class_id'>$class_name</option>";
}

但不是在你的循环中执行它,你可以只进行一次获取所有类名的查询。

$class_ids = array_column($sub_cls, 'class_id');
$run = mysqli_query($conn, "select class_id, class_name from class where class_id in (" . implode(',', $class_ids) . ")");
while ($row = mysqli_fetch_assoc($run)) {
    $class_id = $row['class_id'];
    $class_name = $row['class_name'];
    echo "<option value='$class_id'>$class_name</option>";
}