簡體   English   中英

以數組形式存儲SQL查詢返回的結果

[英]Storing returned result from sql query in the form of array

我正在嘗試將SQL查詢的結果存儲在數組中。我無法使用該結果,但是我可以使用php的print_r()方法進行打印,並且正在打印-

Array
(
    [0] => Array
        (
            [SNO] => 1
            [chartType] => pie
            [outputValues] => rural_total_m,urban_total_m
            [attributeId] => 10025
            [level] => india
        )
)

我希望它以-的形式存儲在變量或文件中

Array(      
       SNO => 1,
       chartType => pie,
       outputValues => rural_total_m,urban_total_m,
       attributeId => 10025,
       level => india
    )

我嘗試了幾件事,但直到現在我都不想做! :( 謝謝!

確實不知道為什么需要這種類型的東西-但從技術上講是不可能的-數組不能兩次 具有相同名稱 / 相同密鑰的 2個密鑰 所以你不能這樣做。

更新:

如果要管理重復的密鑰-您可以執行以下操作:

將查詢結果存儲在$result

   $result = array();
   $sql = mysql_query("... your sql ...");
   while ($row = mysql_fetch_array($res))
   {
      $result[] = $row;
   }

假設這是$ result的樣子:

Array
(
    [0] => Array
        (
            [SNO] => 1
            [chartType] => pie
            [outputValues] => rural_total_m,urban_total_m
            [attributeId] => 10025
            [level] => india
        )
)

現在按以下方式解析它(或者您可以通過foreach,map等其他任何方式進行解析)

$output = array();
array_walk($result, function ($value, $key) use (&$output) {
    // here the $value is the single array you are looking for. 
    $output[] = $value;
});

print_r($output);

這樣就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM