dqsxsmi3704 2014-02-12 07:49
浏览 36
已采纳

PHP PostgreSQL数组到字符串

I have a problem in my PHP script that uses PostgreSQL for fetching and storing data.

Currently I am using the following code:

 $q = "SELECT id FROM playlisty WHERE id_profilu=1;";
 $r = pg_query($q);
 $arr = pg_fetch_all($r);
 echo '<pre>'; var_dump($arr); echo'</pre>';

The output generated by the above code snippet:

array(4) {
  [0]=>
  array(1) {
    ["id"]=>
    string(4) "2014"
  }
  [1]=>
  array(1) {
    ["id"]=>
    string(4) "1549"
  }
  [2]=>
  array(1) {
    ["id"]=>
    string(4) "1965"
  }
  [3]=>
  array(1) {
    ["id"]=>
    string(4) "2047"
  }
}

However, I would prefer if the output looked something like the following: 2014, 1549, 1965, 2047 (i.e. a simple array of id-s of certain playlists). I also tried using implode (to no anvil), and got Array,Array,Array,Array as a reply.

  • 写回答

4条回答 默认 最新

  • dsiimyoc804955 2014-02-12 07:51
    关注

    Why not just loop it ?

    $str=""; 
    foreach($yourarr as $arr) 
    { 
        $str.=implode(',',$arr).',';
    } 
    echo rtrim($str,','); //"prints" 2014, 1549, 1965, 2047
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?