2017-03-03 81 views
0

我試圖從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

回答

1

您需要設置鍵值,而不是在數組內創建新的數組。

$count_get_airlines = $wpdb->get_var("SELECT COUNT(*) FROM wp_shipping_airlines"); 

$data =[]; 
foreach ($get_airlines as $airlines_data) { 
    $data[$airlines_data->letter_code] = $airlines_data->shipping_airline; 
} 
print_r($data); 
+0

你所有的答案是正確的!我只是想,也嘗試過。感謝你們!在我能接受你的答案之前9分鐘。 :) –

0
$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); 
+0

雖然這段代碼是受歡迎的,並且可能會提供一些幫助,但如果它包含* how *和* why *的解釋](// meta.stackexchange.com/q/114762)解決了這個問題。請記住,你正在爲將來的讀者回答這個問題,而不僅僅是現在問的人!請編輯您的答案以添加解釋,並指出適用的限制和假設。 –

1

請這個嘗試:

$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); 
+0

爲什麼這個答案downvote ..任何人解釋.. –

1

您正在數組的數組,這是不是你想要的。你想創建一個關聯數組。試試這個代碼:

$count_get_airlines = $wpdb->get_var("SELECT COUNT(*) FROM wp_shipping_airlines"); 

foreach ($get_airlines as $airlines_data) { 
    $data[$airlines_data->letter_code] = sprintf('%s (%s)', 
      $airlines_data->shipping_airline, 
      $airlines_data->letter_code 
    ); 
} 
print_r($data);