Home > Uncategorized > postgres array to php array or vice versa

postgres array to php array or vice versa

once i needed method to convert php array to postgres array and vice versa. If you need this then here you are 🙂

function postgres_to_php_array($postgresArray){

$postgresStr = trim($postgresArray,”{}”);
$elmts = explode(“,”,$postgresStr);
return $elmts;

}

function php_to_postgres_array( $phpArray){

return “{“.join(“,”,$phpArray).”}”;

}

$tmp = array(“a”,”b”);
print_r ( postgres_to_php_array(php_to_postgres_array($tmp)))

Advertisements
  1. July 16, 2013 at 5:31 pm

    Congratulations for the post, loved the blog.

  2. May 9, 2015 at 1:26 pm

    como seria aqui {“A,C”,ERT}

    • May 9, 2015 at 1:33 pm

      lo solucione usando: str_getcsv(trim(‘{“A,C”,ERT}’, ‘{}’));

  3. praveen
    November 4, 2015 at 7:42 am

    How i convert multi dimensional postgres array to php multidimensional array

  4. Koyae
    June 30, 2016 at 11:34 am

    A safer way to go from a PHP-style array to a Postgres-style one would be:

    // establish a connection using pg_connect() first, then:
    $pgarray = join( array_map(‘pg_escape_string’, $array) );
    $pgarray = “{” . $pgarray . “}”

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: