0

In database postgresql i have field as INTEGER[]. In there i have for example data like this: {2,3,55,122} When i take this data from DB

$rs = getData();
$abc = $rs['raw'];
var_dump($abc);

output is

array(1) {
  [0]=>
  string(18) "{2,3,55,122}"
}

but i need data as array like this:

array(3) {
  [0]=>
  string(2) "2"
  [1]=>
  string(2) "3"
  [2]=>
  string(2) "55"
  [3]=>
  string(2) "122"
}

How can i do it from database layer or php. Is one way use trim and explode only ?

up getData function in ZEND

private function getData(){
$select = $this->objDB->select()
->from(array('tb' => 'table'), array('raw',))
->where('id = 10');
return $this->select($select,true);
}
4
  • Post the getData() function or at least how you build your select query Commented Jun 13, 2014 at 14:35
  • i added getData function Commented Jun 13, 2014 at 14:42
  • Although for this question, you can handle it with PHP quickly. I think you'd better know more about how Array type in PostgreSQL works. For your references: postgresql.org/docs/8.4/static/arrays.html#ARRAYS-DECLARATION Commented Jun 13, 2014 at 14:47
  • so maybe better will be create field as varchar not like integer[] ? Commented Jun 13, 2014 at 14:52

1 Answer 1

1

It's a 2-step process that is easily solved with built in functions:

$abc = explode( ',' ,str_replace( array('{', '}') , '',$abc)) ;

Remove the brackets from the string then explode.

OR as you said you can use trim:

$abc = explode( ',' , trim($abc , '{}')) ;
0

Your Answer

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.