将Postgresql hstore转换为php数组

编程入门 行业动态 更新时间:2024-10-28 21:22:38
本文介绍了将Postgresql hstore转换为php数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有一个很好的php代码片段将postgresql hstore转换为php数组,可以将hstore中未引用的NULL正确转换为php NULL?

Is there a good php code snippet to convert a postgresql hstore to a php array, that will correctly translate an unquoted NULL within the hstore to a php NULL?

EG:假设我们有以下hstore字符串:

EG: Suppose we have the following hstore string:

"k1"=>"v1", "k2"=>NULL, "k3"=>"NULL", "k4"=>"\"v4" (aka SELECT '"k1"=>"v1","k2"=>NULL,"k3"=>"NULL","k4"=>"\\"v4"'::hstore;)

如何将其转换为以下php数组?

How can we convert this into the following php array?

array('k1'=>'v1','k2'= > NULL,'k3'=>'NULL','k4'=>'\ v4');

array('k1' => 'v1', 'k2' => NULL, 'k3' => 'NULL', 'k4' => '\"v4');

我跟随以下转换器,但似乎没有处理未引用的NULL: github/chanmix51/Pomm/blob/ master / Pomm / Converter / PgHStore.php

I following the following converter but it does not seem to handle the unquoted NULL: github/chanmix51/Pomm/blob/master/Pomm/Converter/PgHStore.php

推荐答案

我相信语法会是这样的:

I believe the syntax would be something like this:

$pdo = new PDO( /*connection string*/ ); // h is the hstore column. $stmt = $pdo->query( "SELECT (each(h)).key, (each(h)).value FROM <table name>" ); $output = array(); foreach( $stmt->fetchAll( PDO::FETCH_NUM ) as $row ) { // $row[ 0 ] is the key, $row[ 1 ] is the value. $output[ $row[ 0 ] ] = $row[ 1 ]; }

更多推荐

将Postgresql hstore转换为php数组

本文发布于:2023-10-16 15:44:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498013.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   Postgresql   hstore   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!