note 100257 deleted from ref.pgsql by danbrown
| From: | danbrown@php.net | Date: | Mon, 04 Oct 2010 19:10:09 +0000 |
| Subject: | note 100257 deleted from ref.pgsql by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172950@lists.php.net to get a copy of this message | ||
Note Submitter: n dot can dot kirik at gmail dot com
----
parsing pg arrays tricky. how about parsing arrays of hstores? or hstore values contains arrays?
here is my solution inspired by veggivore at yahoo dot com:
it handles all the variations, etc. array of hstores of arrays...
<?php
function toArray( $input ) {
if ( preg_match( '/^"?[\w\s\d\.]+"?$/', $input ) > 0 ) return $input;
if ( $input == '{}' ) return array();
$input = trim( $input, '{}' );
$output = @eval( 'return array(' . $input . ');' );
if ( $output ) foreach( $output as $k => &$v ) $v = toArray( $v );
if ( !$output ) $output = $input;
return $output;
}
?>
here there is a Turkish edition:
<?php
function diziyeÃevir( $girdi ) {
if ( preg_match( '/^"?[\w\s\d\.]+"?$/', $girdi ) > 0 ) return $girdi;
if ( $girdi == '{}' ) return array();
$girdi = trim( $girdi, '{}' );
$çıktı = @eval( 'return array(' . $girdi . ');' );
if ( $çıktı ) foreach( $çıktı as $k => &$v ) $v = diziyeÃevir( $v );
if ( !$çıktı ) $çıktı = $girdi;
return $çıktı;
}
?>
(maybe regexp won't fit for you.)