简体   繁体   中英

How to group the For-each loop values and execute based on grouped values

Sorry But I am new in development so after a lots of time i didn't get the proper solution.

I wanted to group the array having same values. For Example total size of array is 3 as given below and it will execute 3 times in for each loop but i wanted to execute the loop only 2 time as it contain only 2 unique class_id .

So According to me as output i want to execute the for each only 2 times as it contain 2 unique 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 Code

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>";
        }
    }
}

Alright, so the easiest approach is to make a new array of the values merged together.

// 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;

}

The output is:

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)

Use another array to keep track of which class ID you've already processed.

$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>";
}

But instead of doing it in your loop, you could just do a query that gets all the class names at once.

$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>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM