Re: How can I parse a Postgresql array result in php? - Mailing list pgsql-general

From jco@cornelius-olsen.dk
Subject Re: How can I parse a Postgresql array result in php?
Date
Msg-id OF404B70A6.F5103E59-ONC1256CCD.00481A2D@dk
Whole thread Raw
List pgsql-general
In PHP a Postgres array is represented as text. The trick is to convert this text to a PHP array. Use postgresArrayToArray:

   //Helper function - not for direct calling
 function _pgSubArray($pgArray, &$i)
 {
  if ($pgArray[$i]!='{')
   return NULL;
  $array=array();
  $i++;
  $length=strlen($pgArray);
  while ($i<$length)
  {
   switch ($pgArray[$i])
   {
    case '{':
     array_push($array, _pgSubArray($pgArray, &$i));
     break;
    case '}':
     $i++;
     return $array;
    default:
     $sep=strpos($pgArray, ',', $i);
     $end=strpos($pgArray, '}', $i);
     if ($sep<$end && $sep!==false)
     {
      array_push($array, substr($pgArray, $i, $sep-$i));
      $i=$sep+1; //Skip separator
     }
     else
     {
      array_push($array, substr($pgArray, $i, $end-$i));
      $i=$end;
     }
   }
  }
 }
 
 function postgresArrayToArray($pgArray)
 {
  $i=0;
  return _pgSubArray($pgArray, $i);
 }


14. februar 2003 07:15
To: grant@conprojan.com.au, beloshapka@mnogo.ru, hal@faams.net, jco@cornelius-olsen.dk, pgsql-general@postgresql.org, jwong@digitalview.com.hk, hvp3a@cs.virginia.edu, sugiana@rab.co.id, jakarta@jakarta.linux.or.id, jjm152@hotmail.com, stevenj@nwanews.com, rdkurth@starband.net, chris@wild.net, askwar@a-message.de
cc:
From: "Martin Kuria" <martinkuria@hotmail.com>
Subject: How can I parse a Postgresql array result in php?


Hi,

I have created a table:

CREATE TABLE SAL_EMP (
     name text,
     pay_by_quarter int4[], );

INSERT INTO SAL_EMP VALUES ('Bill', '{10000, 20000, 30000, 40000}');

SELECT pay_by_quarter[1] FROM SAL_EMP WHERE pay_by_quarter[1] = '10000';
+-------------------+
| pay_by_quarter[1] |
+-------------------+
|         10000            |
+-------------------+
My question is I would like to use php to parse this results how do I go about it I have tried using:
$ps = pg_result(result_set, 0, pay_by_quarter[1]);
it does not work please how do I get each value from the array using php
Please assist
Martin Wainaina Kuria
Programmer/Database Administrator


MSN 8 helps ELIMINATE E-MAIL VIRUSES. Get 2 months FREE*.

pgsql-general by date:

Previous
From: Martijn van Oosterhout
Date:
Subject: Re: Postgres Databases growing without much reason
Next
From: Andreas Rust
Date:
Subject: Re: Postgres Databases growing without much reason