1

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 Answers 4

1

Why not just loop it ?

$str=""; 
foreach($yourarr as $arr) 
{ 
    $str.=implode(',',$arr).',';
} 
echo rtrim($str,','); //"prints" 2014, 1549, 1965, 2047
4
  • When i loop it can i use it in "SELECT * FROM table WHERE id IN ($str)" ?
    – M.Stark
    Feb 12, 2014 at 7:52
  • I don't have an idea what results that query would return...By the way did you try the code which I gave you ? Feb 12, 2014 at 7:55
  • Yes, for me the result is "2014154919652047" - as You can see there is no commas
    – M.Stark
    Feb 12, 2014 at 7:57
  • $q = "SELECT id FROM playlisty WHERE id_profilu=1 "; $r = pg_query($q); $yourarr = pg_fetch_all($r); $str=""; foreach($yourarr as $arr) { $str.=implode(',',$arr); } echo $str;
    – M.Stark
    Feb 12, 2014 at 8:14
1

Add this line before echo:

$arr = array_map(function ($v) { return $v['id']; }, $arr);

If you want to output result as string then you have to use implode function. Replace last line with this:

echo '<pre>'. implode(', ', $arr). '</pre>';
2
  • 1
    it returned "array(4) { [0]=> string(4) "2014" [1]=> string(4) "1549" [2]=> string(4) "1965" [3]=> string(4) "2047" }"
    – M.Stark
    Feb 12, 2014 at 7:54
  • If you want to output result as string you have to use implode function (php.net/manual/en/function.implode.php) instead of var_dump: echo '<pre>'; implode(', ', $arr); echo'</pre>';
    – hindmost
    Feb 12, 2014 at 8:01
0

Try this:

$array = array(
            0=>array('id'=>"2014"),
            1=>array('id'=>"2015"),
            2=>array('id'=>"2016"),
        );
        $ids = array_map(function($item) { return $item['id']; }, $array);
        $result = implode($ids,',');
        var_dump($result);

Output: string '2014,2015,2016' (length=14)

0

Do select like this:

$q = "SELECT array_to_string(id, ',') AS id FROM playlisty WHERE id_profilu=1 ";
2
  • and how do i get the result from this ?
    – M.Stark
    Feb 12, 2014 at 8:13
  • Like this: $arr[0]['id'] will return 2014,2015,2016[,...]. You don't have need to implode using php.
    – Vin.AI
    Feb 12, 2014 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.