将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 ];
}