PHP array to postgres array -
now php can't work directly wit postgresql array. example, php taking postgresql array '{"foo","bar"}'
i need simple php function create multidimensional postgresql array php array.
i think experimental pg_convert() isn't optimal because needs of data form simple array string database output, maybe misunderstood idea of function.
for example, need convert
$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"}}'; can use array_walk_recursive() convert deepest elements of array?
here's simple function converting php array pg array.
function to_pg_array($set) { settype($set, 'array'); // can called scalar or array $result = array(); foreach ($set $t) { if (is_array($t)) { $result[] = to_pg_array($t); } else { $t = str_replace('"', '\\"', $t); // escape double quote if (! is_numeric($t)) // quote non-numeric values $t = '"' . $t . '"'; $result[] = $t; } } return '{' . implode(",", $result) . '}'; // format }
Comments
Post a Comment