Re: Associative array in Pl/PgSQL - Mailing list pgsql-general

From Darren Duncan
Subject Re: Associative array in Pl/PgSQL
Date
Msg-id 5185BC90.5070702@darrenduncan.net
Whole thread Raw
In response to Associative array in Pl/PgSQL  (Karel Riveron Escobar <kescobar@estudiantes.uci.cu>)
List pgsql-general
On 2013.05.04 4:40 PM, Karel Riveron Escobar wrote:
> Hello everyone,
>
> I have a problem with Pl/PgSQL function. I need to pass it as parameter an
> associative array. I have no idea how to do that. Can somebody help me?
>
> To be more specific, I have an associative array in PHP. Something like this:
>
> array(
>       'name' => 'Robert',
>       'age' => 24,
>       'teachers' => array(
>                                     array('name'=>'Sean'),
>                                     array('name'=>'Megan')
>                              )
> );
>
> And, I need work with it into a Pl/PgSQL function. How can I do this?

The answer to your question depends on a number of factors about the structure
of your associative array.

If the structure is heterogeneous, where each element might be a different data
type, and if the keys of your array are all text analogous to attribute names,
then a SQL tuple/row is the most direct analogy, so use some ROW type with one
attribute per associative array element.

Conversely, if the structure is homogeneous, where all elements have the same
key types and same value types, then a binary/2-attribute/column relation/table
is the most direct analogy, so then use a TABLE(key text,value whatever) type.

 From your example though, it looks like a ROW is definitely what you want.

-- Darren Duncan



pgsql-general by date:

Previous
From: Jasen Betts
Date:
Subject: Re: Associative array in Pl/PgSQL
Next
From: Steve Rogerson
Date:
Subject: Composite fields and the query planner