繁体   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