国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

按照ID對MySQL結(jié)果進行分組以便循環(huán)遍歷
P粉638343995
P粉638343995 2024-04-06 17:27:56
0
1
1006

我有一個表,其中有 mysql 中的航班數(shù)據(jù)。我正在編寫一個 php 代碼,它將使用 codeigniter 3 對數(shù)據(jù)進行分組和顯示

journey_id     air_id  FlightDuration  out_or_in   flightduration2
    1           1           20hr 5min   outbound    1205
    2           1           20hr 5min   outbound    1300
    3           1           17hr 55min  inbound     2258
    4           1           17hr 55min  inbound     1075
    5           2           31hr 40min  outbound    1970
    6           2           31hr 40min  outbound    1900
    7           2           17hr 55min  inbound     2223
    8           2           17hr 55min  inbound     1987
    9           3           10hr 45min  outbound    645
    10          3           11hr 25min  inbound     685

我使用 $this->db->get() 來檢索數(shù)據(jù),我可以輕松循環(huán)。但由于每一行都在數(shù)組中,我發(fā)現(xiàn)很難將它們分組。我無法使用 mysql 組,因為我需要每一行。

舉個例子,我想顯示如下的項目

air_id - 1
20hr 5min   outbound    1205
20hr 5min   outbound    1300
17hr 55min  inbound     2258
17hr 55min  inbound     1075

air_id - 2
31hr 40min  outbound    1970
31hr 40min  outbound    1900
17hr 55min  inbound     2223
17hr 55min  inbound     1987

air_id - 3
10hr 45min  outbound    645
11hr 25min  inbound     685

通過 air_id 對結(jié)果進行分組的最佳方法是什么,以便我可以迭代

P粉638343995
P粉638343995

全部回復(1)
P粉043295337
  1. 從數(shù)據(jù)庫中獲取數(shù)據(jù):

    $this->db->select('journey_id, air_id, FlightDuration, out_or_in, flightduration2');
     $this->db->from('your_table_name'); // Replace 'your_table_name' with the actual table name
     $query = $this->db->get();
     $data = $query->result_array();
  2. 創(chuàng)建一個空數(shù)組來保存分組數(shù)據(jù):

    $grouped_data = array();
  3. 迭代獲取的數(shù)據(jù)并按air_id對其進行分組:

    foreach ($data as $row) {
    
        $air_id = $row['air_id'];
    
        // Check if the air_id already exists in the grouped_data array
        if (!isset($grouped_data[$air_id])) {
            // If not, initialize an empty array for this air_id
            $grouped_data[$air_id] = array();
        }
    
        // Add the current row to the group for this air_id
        $grouped_data[$air_id][] = $row;
    }
  4. 現(xiàn)在,您已在 $grouped_data 數(shù)組中按 air_id 分組了數(shù)據(jù)。您可以循環(huán)訪問此數(shù)組以顯示您指定的數(shù)據(jù):

    foreach ($grouped_data as $air_id => $group) {
        echo "air_id - $air_id
    "; foreach ($group as $row) { echo $row['FlightDuration'] . ' ' . $row['out_or_in'] . ' ' . $row['flightduration2'] . '
    '; } echo "
    "; }

此代碼將循環(huán)遍歷分組數(shù)據(jù)并按照您的描述進行顯示,每組航班數(shù)據(jù)都在相應(yīng)的air_id下。

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板