将Postgresql hstore转换为php数组 [英] Convert postgresql hstore to php array

查看:86
本文介绍了将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: https://github.com/chanmix51/Pomm/blob/ master / Pomm / Converter / PgHStore.php

I following the following converter but it does not seem to handle the unquoted NULL: https://github.com/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数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆