Re: How can I parse a Postgresql array result in php?

From: jco(at)cornelius-olsen(dot)dk
To: "Martin Kuria" <martinkuria(at)hotmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How can I parse a Postgresql array result in php?
Date: 2003-02-14 13:09:09
Message-ID: OF404B70A6.F5103E59-ONC1256CCD.00481A2D@dk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)conprojan(dot)com(dot)au, beloshapka(at)mnogo(dot)ru, hal(at)faams(dot)net, jco(at)cornelius-olsen(dot)dk, pgsql-general(at)postgresql(dot)org, jwong(at)digitalview(dot)com(dot)hk, hvp3a(at)cs(dot)virginia(dot)edu, sugiana(at)rab(dot)co(dot)id, jakarta(at)jakarta(dot)linux(dot)or(dot)id, jjm152(at)hotmail(dot)com, stevenj(at)nwanews(dot)com, rdkurth(at)starband(dot)net, chris(at)wild(dot)net, askwar(at)a-message(dot)de
cc:
From: "Martin Kuria" <martinkuria(at)hotmail(dot)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*.

Browse pgsql-general by date

  From Date Subject
Next Message Andreas Rust 2003-02-14 13:12:12 Re: Postgres Databases growing without much reason
Previous Message Martijn van Oosterhout 2003-02-14 13:03:04 Re: Postgres Databases growing without much reason