我試圖從MySQL查詢中使用foreach添加循環內的所有數據。數據包括密鑰和值。但在我的代碼中,它是在我想要的數組之前用整數鍵包圍的。在循環中設置密鑰和值
下面的代碼:
$count_get_airlines = $wpdb->get_var("SELECT COUNT(*) FROM wp_shipping_airlines");
foreach ($get_airlines as $airlines_data) {
$data[] = [$airlines_data->letter_code => $airlines_data->shipping_airline];
}
print_r($data);
如下的結果:
Array (
[0] => Array ([CA] => Air China)
[1] => Array ([PX] => Air Niugini)
[2] => Array ([OZ] => Asiana Airlines)
[3] => Array ([CI] => China Airlines)
[4] => Array ([MU] => China Eastern Airlines)
[5] => Array ([CZ] => China Southern Airlines)
[6] => Array ([ET] => Ethiopian Airlines)
[7] => Array ([EY] => Etihad)
[8] => Array ([BR] => Eva Air)
[9] => Array ([GF] => Gulf Air)
[10] => Array ([JL] => Japan Airlines)
[11] => Array ([7C] => Jeju Air)
[12] => Array ([KE] => Korean Airlines)
[13] => Array ([KU] => Kuwait Airlines)
[14] => Array ([MU] => Malaysian Airlines)
[15] => Array ([WY] => Oman Air)
[16] => Array ([QF] => Qantas Airlines)
[17] => Array ([QR] => Qatar Airways)
[18] => Array ([BI] => Royal Brunei)
[19] => Array ([SV] => Saudia Airlines)
[20] => Array ([TG] => Thai Airways)
[21] => Array ([TR] => Tiger Airways)
[22] => Array ([TK] => Turkish Airlines)
[23] => Array ([UA] => United Airlines)
[24] => Array ([MF] => Xiamen Airlines)
[25] => Array ([PR] => Philippine Airlines)
[26] => Array ([DL] => Delta Air Lines)
[27] => Array ([CX] => Cathay Pacific Airways)
[28] => Array ([SQ] => Singapore Airlines)
[29] => Array ([NH] => All Nippon Airways)
[30] => Array ([EA] => Emirates Airlines)
[31] => Array ([AK] => AirAsia Phils.)
[32] => Array ([5J] => Cebu Pacific)
);
我想要什麼:
Array(
'CA'=> 'Air China (CA)',
'PX'=> 'Air Niugini (PX)',
'OZ'=> 'Asiana Airlines (OZ)',
'CI'=> 'China Airlines (CI)',
'MU'=> 'China Eastern Airlines (MU)',
'CZ'=> 'China Southern Airlines (CZ)',
'ET'=> 'Ethiopian Airlines (ET)',
'EY'=> 'Etihad (EY)',
'BR'=> 'Eva Air (BR)',
'GF'=> 'Gulf Air (GF)',
'JL'=> 'Japan Airlines (JL)',
'7C'=> 'Jeju Air (7C)',
'KE'=> 'Korean Airlines (KE)',
'KU'=> 'Kuwait Airlines (KU)',
'MU'=> 'Malaysian Airlines (MU)',
'WY'=> 'Oman Air (WY)',
'QF'=> 'Qantas Airlines (QF)',
'QR'=> 'Qatar Airways (QR)',
'BI'=> 'Royal Brunei (BI)',
'SV'=> 'Saudia Airlines (SV)',
'TG'=> 'Thai Airways (TG)',
'TR'=> 'Tiger Airways (TR)',
'TK'=> 'Turkish Airlines (TK)',
'UA'=> 'United Airlines (UA)',
'MF'=> 'Xiamen Airlines (MF)',
'PR' => 'Philippine Airlines (PR)',
'AK' => 'AirAsia Phils. (AK)',
'NH' => 'All Nippon Airways (NH)',
'CX' => 'Cathay Pacific Airways (CX)',
'5J' => 'Cebu Pacific (5J)',
'DL' => 'Delta Air Lines (DL)',
'EA' => 'Emirates Airlines (EA)',
'SQ' => 'Singapore Airlines (SQ)'
);
我缺少什麼?我究竟做錯了什麼? 任何幫助是高度讚賞。
謝謝! -Eli
你所有的答案是正確的!我只是想,也嘗試過。感謝你們!在我能接受你的答案之前9分鐘。 :) –