Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8527791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:39:19+00:00 2026-06-11T08:39:19+00:00

I have two array values of the same length in PostgreSQL: {a,b,c} and {d,e,f}

  • 0

I have two array values of the same length in PostgreSQL:

{a,b,c} and {d,e,f}

and I’d like to combine them into

{{a,d},{b,e},{c,f}}

Is there a way to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:39:21+00:00Added an answer on June 11, 2026 at 8:39 am

    Postgres 9.5 or later

    has array_agg(array expression):

    array_agg ( anyarray ) → anyarray

    Concatenates all the input arrays into an array of one higher
    dimension. (The inputs must all have the same dimensionality, and
    cannot be empty or null.)

    This is a drop-in replacement for my custom aggregate function array_agg_mult() demonstrated below. It’s implemented in C and considerably faster. Use it.

    Postgres 9.4

    Use the ROWS FROM construct or the updated unnest() which takes multiple arrays to unnest in parallel. Each can have a different length. You get (per documentation):

    […] the number of result rows in this case is that of the largest function
    result, with smaller results padded with null values to match.

    Use this cleaner and simpler variant:

    SELECT ARRAY[a,b] AS ab
    FROM   unnest('{a,b,c}'::text[] 
                , '{d,e,f}'::text[]) x(a,b);
    

    Postgres 9.3 or older

    Simple zip()

    Consider the following demo for Postgres 9.3 or earlier:

    SELECT ARRAY[a,b] AS ab
    FROM  (
       SELECT unnest('{a,b,c}'::text[]) AS a
            , unnest('{d,e,f}'::text[]) AS b
        ) x;
    

    Result:

      ab
    -------
     {a,d}
     {b,e}
     {c,f}
    

    Note that both arrays must have the same number of elements to unnest in parallel, or you get a cross join instead.

    You can wrap this into a function, if you want to:

    CREATE OR REPLACE FUNCTION zip(anyarray, anyarray)
      RETURNS SETOF anyarray LANGUAGE SQL AS
    $func$
    SELECT ARRAY[a,b] FROM (SELECT unnest($1) AS a, unnest($2) AS b) x;
    $func$;
    

    Call:

    SELECT zip('{a,b,c}'::text[],'{d,e,f}'::text[]);
    

    Same result.

    zip() to multi-dimensional array:

    Now, if you want to aggregate that new set of arrays into one 2-dimenstional array, it gets more complicated.

    SELECT ARRAY (SELECT ...)

    or:

    SELECT array_agg(ARRAY[a,b]) AS ab
    FROM  (
       SELECT unnest('{a,b,c}'::text[]) AS a
             ,unnest('{d,e,f}'::text[]) AS b
        ) x

    or:

    SELECT array_agg(ARRAY[ARRAY[a,b]]) AS ab
    FROM  ...

    will all result in the same error message (tested with pg 9.1.5):

    ERROR: could not find array type for data type text[]

    But there is a way around this, as we worked out under this closely related question.
    Create a custom aggregate function:

    CREATE AGGREGATE array_agg_mult (anyarray) (
       SFUNC    = array_cat
     , STYPE    = anyarray
     , INITCOND = '{}'
    );
    

    And use it like this:

    SELECT array_agg_mult(ARRAY[ARRAY[a,b]]) AS ab
    FROM  (
       SELECT unnest('{a,b,c}'::text[]) AS a
            , unnest('{d,e,f}'::text[]) AS b
        ) x
    

    Result:

    {{a,d},{b,e},{c,f}}
    

    Note the additional ARRAY[] layer! Without it and just:

    SELECT array_agg_mult(ARRAY[a,b]) AS ab
    FROM ...
    

    You get:

    {a,d,b,e,c,f}
    

    Which may be useful for other purposes.

    Roll another function:

    CREATE OR REPLACE FUNCTION zip2(anyarray, anyarray)
      RETURNS SETOF anyarray LANGUAGE SQL AS
    $func$
    SELECT array_agg_mult(ARRAY[ARRAY[a,b]])
    FROM (SELECT unnest($1) AS a, unnest($2) AS b) x;
    $func$;
    

    Call:

    SELECT zip2('{a,b,c}'::text[],'{d,e,f}'::text[]); -- or any other array type
    

    Result:

    {{a,d},{b,e},{c,f}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays of values like X,Y,Z and 1,2 There is a table
I have two arrays values and keys both of the same length. I want
I have two js arrays already, say: names and values (with the same length),
I have two sequential (non-associative) arrays whose values I want to combine into a
I have two arrays with time values in them in this format. 00:00:00 which
I have two array and three button in segment control i want that when
Say I have an $input array, that contains something like this : array 0
I have two tables on my html page with exact same data but there
I have two ViewController. The first contains and displays an array with values. The
I have built a custom hashmap using two arrays. One contains keys another values.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.