douxiezha9319 2017-05-19 09:49
浏览 129
已采纳

从postgres中检索数组

Very basic question but couldn´t find a solution.

In a table with some values I try to store in 1 column a array (int[]) and retrieve it. The storing and searching works fine but if I select it I get it as string in php.

Table week

col id (int) = 1
col days (int[]) = {1,1,1,1,1,0,0}

PHP

$query = SELECT id, days, manyother FROM week //array_to_json(days) does the same result
$pdo->setAttribute( PDO::ATTR_CASE, PDO::CASE_NATURAL );
$result = $pdo->query($query);

$test = $result->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($test)

Returns:

id: 1
days: "[1,1,1,1,1,0,0]"
manyother: ""

I´m sure I miss just something with json_encode/decode Edit: its not affected by json_encode, I debuged it and before it returns the value like '[1,1,1,1,1,0,0]'.

Edit 2: Found a solution which works but increases the loading time 15* times :D So the question is still open but I have a workaround for the moment.

foreach($test as $key => $value){
        $test[$key]['days'] = json_decode($value['days']);
}
  • 写回答

1条回答 默认 最新

  • drpiqlzrh62917192 2017-05-19 09:54
    关注

    when selecting array, you can hack the way array is displayed:

    t=# select json_build_array(array[1,2,3,4])->0;
     ?column?
    -----------
     [1,2,3,4]
    (1 row)
    

    It should be easily evaluated by php then with eval. Or even json_agg on whole data set and then eval:

    t=# select json_agg(s162) from s162;
    json_agg
    [{"i":0,"a":[1,2,3,4]},
     {"i":1,"a":[1,4]}]
    (1 row)
    Time: 0.281 ms
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料