Re: Associative array in Pl/PgSQL

From: Darren Duncan <darren(at)darrenduncan(dot)net>
To: pgsql-general(at)postgresql(dot)org
Cc: Karel Riveron Escobar <kescobar(at)estudiantes(dot)uci(dot)cu>
Subject: Re: Associative array in Pl/PgSQL
Date: 2013-05-05 01:57:36
Message-ID: 5185BC90.5070702@darrenduncan.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Steve Rogerson 2013-05-05 13:05:26 Composite fields and the query planner
Previous Message Jasen Betts 2013-05-05 00:13:26 Re: Associative array in Pl/PgSQL