PHP数组Postgres的数组 [英] PHP array to postgres array

查看:222
本文介绍了PHP数组Postgres的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在PHP不能直接工作,机智PostgreSQL的数组。例如,PHP以PostgreSQL的阵列状
    {富,酒吧}

Now php can't work directly wit Postgresql array. For example, php taking postgresql array like '{"foo","bar"}'

我需要简单的PHP函数来创建PHP数组多维数组的PostgreSQL

I need simple php function to create multidimensional postgresql array from php array.

我认为,因为它需要额外的数据,形成简单的字符串数组数据库输出实验pg_convert()不是最优的,也许是我误解了这个功能的想法。

I think that experimental pg_convert() isn't optimal because it needs of extra data to form simple array string for database output, maybe I misunderstood the idea of this function.

例如,我需要转换

$from=array(  array( "par_1_1","par_1_2" ), array( "array_2_1", "array_2_2" )  );
$to='{{"par_1_1","par_1_2"},{"par_2_1","par_2_2"}}';

我可以使用array_walk_recursive()数组的元素最深的转换?

Can I use array_walk_recursive() to convert the deepest elements of array?

推荐答案

下面是一个简单的功能PHP数组转换为PG阵列。

Here's a simple function for converting a PHP array to PG array.

function to_pg_array($set) {
    settype($set, 'array'); // can be called with a scalar or array
    $result = array();
    foreach ($set as $t) {
        if (is_array($t)) {
            $result[] = to_pg_array($t);
        } else {
            $t = str_replace('"', '\\"', $t); // escape double quote
            if (! is_numeric($t)) // quote only non-numeric values
                $t = '"' . $t . '"';
            $result[] = $t;
        }
    }
    return '{' . implode(",", $result) . '}'; // format
}

这篇关于PHP数组Postgres的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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