I'm having trouble with my php script to trim the whitespace from within values as i output the results of a query from postgresql. the commented line is the returned array from a print_r();
<?php
$db = pg_connect("host=localhost dbname=test user=test password=test");
$result = pg_query($db, "SELECT * FROM table WHERE field = 'test'");
$array = pg_fetch_all($result);
//Array ( [0] => Array ( [id_number] => 214 [country] => Zanzibar [sector] => Unguja [site] => Chumbe Island Coral Park (CHICOP) [username] => n ) [1] => Array ( [id_number] => 213 [country] => Zanzibar [sector] => Unguja [site] => Chumbe Island Coral Park (CHICOP) [username] => n )
//*************
$array_explode = explode(",", $array);
$array_trimmed = array_map("trim", $array_explode);
$array_implode = implode(",", $array_trimmed);
unset($array_explode);
//*************
$fp = fopen('file.csv', 'w');
foreach ($array_implode as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
If i remove the lines between the stars then the csv outputs (just with a whole lot of unnecessary whitespace. I have tried both with and without the implode()
. this has been driving me up the wall and any help would be greatly appreciated.