0

This topic may have already been posted, but i didn't find it ...

With postgresql, i return an array with a function i made :

Select * from my_function(var1,var2);

And i got the expected result :

{x,y}

By using php (pg_fetch_array($result)), i also manage to get this result in this form. What i would like to get, now, is x and y separately. Like this

{x},{y}

Is that possible ? I wanted to know how can i get element one by one from an array ?

Navy07
  • 27
  • 5
  • Can you use `print_r($result)`? – aldrin27 Aug 29 '15 at 00:08
  • I already tried this, and it gave me this result : Resource id #42 , which is normal, given that what i point to in print_r is a case, not the contain ... – Navy07 Aug 29 '15 at 00:11
  • Um, what do you expect with doing so ? I mean, var_dump or print_r are only for showing a result, no ? What I want is passing from **{x,y}** to **{x},{y}** – Navy07 Aug 29 '15 at 00:15
  • Use `explode(',', $result)` then store that in `$newVar = []` – aldrin27 Aug 29 '15 at 00:19
  • i got nothing ... but maybe it's because of my code, though i don't understand why ... anyway, thank you for having been there ! – Navy07 Aug 29 '15 at 00:25
  • Maybe the result is an object array. – aldrin27 Aug 29 '15 at 00:30
  • possible duplicate of [PHP Assign array to variables](http://stackoverflow.com/questions/3340750/php-assign-array-to-variables) – Ryne Everett Aug 29 '15 at 00:35
  • @aldrin27 `my_function(var1,var2)` returns a VARCHAR[] : it's an array, no ? if it was an object though, how could i proceed so ? – Navy07 Aug 29 '15 at 00:39
  • @RyneEverett thanks ! it seems my problem is similiar ... – Navy07 Aug 29 '15 at 00:40
  • thank you to you too, @aldrin27 – Navy07 Aug 29 '15 at 00:41
  • Your function should return two variables (a row: `(x, y)`) and not an array to begin with. Then just call it like you already do: `SELECT * from my_function(var1,var2);` While returning only a single array value, you can as well just: `SELECT my_function(var1,var2);` – Erwin Brandstetter Aug 29 '15 at 23:56

1 Answers1

0

It sounds like you want a resultset. You can do this in SQL with unnest:

SELECT unnested.elem 
FROM my_function(var1,var2) f(arr),
     unnest(f.arr) unnested(elem);
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778