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

本文是小编为大家收集整理的关于将postgresql hstore转换为php数组的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

是否有良好的PHP代码片段将PostgreSQL Hstore转换为PHP数组,它将正确地将Hstore中的无引用的null转换为php null?

eg:假设我们有以下Hstore字符串:

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

我们如何将其转换为以下PHP数组?

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

i遵循以下转换器,但似乎无法处理未引用的null: https ://github.com/chanmix51/pomm/blob/master/pomm/pomm/converter/pghstore.php

推荐答案

我相信语法将是这样的:

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

其他推荐答案

$hstore = '"A"=>"AAA", "B"=>"BBB"';
print_r(json_decode('{' . str_replace('"=>"', '":"', $hstore) . '}', true));

其他推荐答案

我试图使用Pomm的PGHStore方法,但是它在六个左右的情况下破裂了.我不记得他们全部,但是这里有一些我记得:

  • 缺乏本地php null支持
  • 缺乏正确逃脱双引号
  • 没有适当地逃脱安全后Ql插入的值

我最终最终得出了自己的解决方案,即创建的phpg.支持任何数据类型,Hstores,几何数据类型,日期/时间戳等阵列的自动转换:

如果您想在不使用外部班级或图书馆的情况下解决此问题,这将是非常困难的,并且很可能很可靠,这就是为什么:

有人可能建议您简单地在","上爆炸以创建一个键/值对的列表,您可以从中爆炸"=>"上的列表中的每个元素.如果所有值是字符串,则可以使用,但是PostgreSQL Hstores支持NULL值.无零值不会封装在双引号中,因此上述解决方案在这些情况下无法适当起作用.

本文地址:https://www.itbaoku.cn/post/1763744.html

问题描述

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: 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;)

How can we convert this into the following php array?

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

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

其他推荐答案

$hstore = '"A"=>"AAA", "B"=>"BBB"';
print_r(json_decode('{' . str_replace('"=>"', '":"', $hstore) . '}', true));

其他推荐答案

I attempted to make use of Pomm's PgHStore method, however it broke under a half dozen or so different circumstances. I don't remember them all, but here are a few I do recall:

  • Lack of native PHP Null support
  • Lack of properly escaping double quotes
  • Didn't properly escape values for safe PostgreSQL insertion

I ultimately ended up deriving my own solution, coined PHPG. Supports automatic conversion of arrays of any data-type, Hstores, Geometric data-types, Dates / Timestamps, etc: https://github.com/JDBurnZ/PHPG

If you wish to tackle this problem without the use of an external class or library, this is going to be very difficult and will most likely be reliable, and here's why:

One might suggest you could simply explode on "," to create a list of key/value pairs, from which you could explode each element in the list on "=>". This works if all values are strings, but PostgreSQL Hstores support NULL values. NULL values are not encapsulated in double quotes, so the above solution will not work appropriately in these scenarios.