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条)

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类