note 102774 deleted from function.implode by danbrown
| From: | danbrown@php.net | Date: | Sat, 05 Mar 2011 14:38:59 +0000 |
| Subject: | note 102774 deleted from function.implode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177579@lists.php.net to get a copy of this message | ||
Note Submitter: xcojack at gmail dot com
----
Here is an example how to convert php array to postgresql anyarray type:
<?php
/**
* @author PrzemysÅaw Czekaj <xcojack@gmail.com>
* @link http://www.cojack.pl
*/
function phpArrayToPg(array $phpArray, $inner_glue = ',') {
foreach($phpArray as $key => $value) {
if(is_array($value)) {
$output[$key] = "{".phpArrayToPg($value, $inner_glue)."}";
}
else {
if(!(int)$value)
$output[$key] = '"'.$value.'"';
else
$output[$key] = (int)$value;
}
}
return implode($inner_glue,$output);
}
?>
Usage?
Some array:
<?php
$a = array(
array(
1,
'cgusia',
),
array(
6,
2
),
array(
'oruflik',
'jrzmiot'
)
);
echo "E'{".phpArrayToPg($a)."}'";
?>
the above example will output:
E'{{1,"cgusia"},{6,2},{"oruflik","jrzmiot"}}'
How you will secure this part of data when you will send them to the sql server it's only your
problem.
Cast them and it works awesome ;)