2016-11-14 57 views
1

我可能使用了不正確白話的說:「陣」,但我有一個SQL查詢我執行的是返回數據如下所示:返回SQL結果簡單數組每列:PHP OCI

HR MAILED FAILED BOUNCED 
06 1442 0 0 
07 825218 185 2433 
08 210672 496 2019 
09 254125 388 2854 
10 159744 366 2725 
11 164515 360 2413 
12 1246917 547 3329 
13 73837 269 1487 

我想有它返回存儲作爲自己的變量,4個獨立的陣列看起來是這樣的:

$var1 = ["hr","mailed","failed","bounced"] 
$var2 = [1442, 825218, 21062, 254125, 159744, 164515, 1246917, 73837] 
$var3 = ...results from 2nd column... 
$var4 = ...results from 3rd column... 

的代碼我目前已經被吐出一個數組,但是這一切都靠不住......

<?php 
include 'includes/connect.php'; 
include 'includes/queries.php'; 

$stid = oci_parse($conn, $mfg_last6); 
oci_execute($stid); 
$ncols = oci_fetch_all($stid, $res, null, null, OCI_FETCHSTATEMENT_BY_COLUMN); 

echo "$ncols rows fetched<br>\n"; 
var_dump($res); 

oci_free_statement($stid); 
oci_close($conn); 

?> 

,給了我的結果...

8 rows fetched 
array(4) { ["HR"]=> array(8) { [0]=> string(2) "06" [1]=> string(2) "07" [2]=> string(2) "08" [3]=> string(2) "09" [4]=> string(2) "10" [5]=> string(2) "11" [6]=> string(2) "12" [7]=> string(2) "13" } ["MAILED"]=> array(8) { [0]=> string(4) "1442" [1]=> string(6) "825218" [2]=> string(6) "210672" [3]=> string(6) "254125" [4]=> string(6) "159744" [5]=> string(6) "164515" [6]=> string(7) "1247164" [7]=> string(6) "123649" } ["FAILED"]=> array(8) { [0]=> string(1) "0" [1]=> string(3) "185" [2]=> string(3) "496" [3]=> string(3) "388" [4]=> string(3) "366" [5]=> string(3) "360" [6]=> string(3) "547" [7]=> string(3) "446" } ["BOUNCED"]=> array(8) { [0]=> string(1) "0" [1]=> string(4) "2433" [2]=> string(4) "2019" [3]=> string(4) "2854" [4]=> string(4) "2725" [5]=> string(4) "2413" [6]=> string(4) "3329" [7]=> string(4) "2352" } } 

我敢肯定它可能很簡單,但我用PHP對於新手和我還是很學習。感謝任何幫助!

謝謝!

回答

1

每列只是$res數組的一個元素。

$var1 = array_keys($res); 
$var2 = $res['MAILED']; 
$var3 = $res['FAILED']; 
$var4 = $res['BOUNCED']; 
+0

謝謝!這讓我PHP數組 - 現在如何將它們轉換爲JavaScript? – Birkley

+0

@Birkley Javascript與這個問題有什麼關係?無論如何,你可以使用'json_encode',例如'var var1 = <?echo json_encode($ var1); ?>;' – Barmar

+0

我的歉意。我給出的數組示例是在JS中,但我沒有具體說出來。我的錯。謝謝你的快速反應。 – Birkley

0

你將不得不做頭(第一列)手動...然後就環通的結果:

$final_array = ["hr","mailed","failed","bounced"]; 
while (($row = oci_fetch_array($stid, OCI_BOTH)) != false) { 
    $final_array["hr"] = $row['hr']; 
    $final_array["mailed"] = $row['mailed']; 
    $final_array["failed"] = $row['failed']; 
    $final_array["bounced"] = $row['bounced']; 
} 

var_dump($final_array);