简体   繁体   English

以数组形式存储SQL查询返回的结果

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

I am trying to store the result of SQL query in an array.I am not able to use that result,however i can print it using print_r() method of php and it is printing- 我正在尝试将SQL查询的结果存储在数组中。我无法使用该结果,但是我可以使用php的print_r()方法进行打印,并且正在打印-

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

I want it to store in a variable or a file in the form of-- 我希望它以-的形式存储在变量或文件中

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

I have tried few thing but nothing like i want till now! 我尝试了几件事,但直到现在我都不想做! :( Thank You! :( 谢谢!

Really not sure why you will need this type of things -- but technically it is not possible -- an array cannot have 2 keys with same name / same key twice . 确实不知道为什么需要这种类型的东西-但从技术上讲是不可能的-数组不能两次 具有相同名称 / 相同密钥的 2个密钥 So you can't do this. 所以你不能这样做。

UPDATE: 更新:

In case of duplicate keys to be managed - you can do something like: 如果要管理重复的密钥-您可以执行以下操作:

Store the query result in $result 将查询结果存储在$result

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

Say this is how the $result looks like: 假设这是$ result的样子:

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

Now parse it following way (or you can do it any other way like foreach, map, etc.) 现在按以下方式解析它(或者您可以通过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);

This will do. 这样就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM