Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);
}
share|improve this question
    
Post the getData() function or at least how you build your select query –  Javad Jun 13 at 14:35
    
i added getData function –  user3733929 Jun 13 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 –  Jack Song Jun 13 at 14:47
    
so maybe better will be create field as varchar not like integer[] ? –  user3733929 Jun 13 at 14:52

1 Answer 1

up vote 1 down vote accepted

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 , '{}')) ;
share|improve this answer
    
Great , thx for help –  user3733929 Jun 13 at 14:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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