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.

jub0bs
67.3k27 gold badges198 silver badges200 bronze badges
asked Feb 12, 2014 at 7:49

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
answered Feb 12, 2014 at 7:51
Sign up to request clarification or add additional context in comments.

4 Comments

When i loop it can i use it in "SELECT * FROM table WHERE id IN ($str)" ?
I don't have an idea what results that query would return...By the way did you try the code which I gave you ?
Yes, for me the result is "2014154919652047" - as You can see there is no commas
$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;
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>';
answered Feb 12, 2014 at 7:53

2 Comments

it returned "array(4) { [0]=> string(4) "2014" [1]=> string(4) "1549" [2]=> string(4) "1965" [3]=> string(4) "2047" }"
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>';
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)

answered Feb 12, 2014 at 7:55

Comments

0

Do select like this:

$q = "SELECT array_to_string(id, ',') AS id FROM playlisty WHERE id_profilu=1 ";
answered Feb 12, 2014 at 7:56

2 Comments

and how do i get the result from this ?
Like this: $arr[0]['id'] will return 2014,2015,2016[,...]. You don't have need to implode using php.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.