You can get each 1D array from a 2D array with SLICE 1 and array_to_json() in a FOREACH statement as shown below, then you can use them for what you want.
DO $$
DECLARE
temp VARCHAR[];
_2d_arr VARCHAR[] := ARRAY[
['one', 'two', 'three'],
['four', 'five', 'six']
];
BEGIN -- ↓ ↓ ↓ ↓
FOREACH temp SLICE 1 IN ARRAY _2d_arr LOOP
RAISE INFO '%', array_to_json(temp);
END LOOP; -- ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
END
$$;
Then, you can get each 1D array from the 2D array as shown below:
INFO: ["one","two","three"]
INFO: ["four","five","six"]
DO
In addition, you can use SLICE 2 as shown below:
DO $$
DECLARE
temp VARCHAR[];
_2d_arr VARCHAR[] := ARRAY[
['one', 'two', 'three'],
['four', 'five', 'six']
];
BEGIN -- ↓ ↓ ↓ ↓
FOREACH temp SLICE 2 IN ARRAY _2d_arr LOOP
RAISE INFO '%', array_to_json(temp);
END LOOP;
END
$$;
Then, you can get the whole 2D array as shown below:
INFO: [["one","two","three"],["four","five","six"]]
DO
And, you can use SLICE 0 without array_to_json() as shown below. *You need to change the type of temp local variable from VARCHAR[] to VARCHAR otherwise there is error:
DO $$
DECLARE
-- temp VARCHAR[];
temp VARCHAR; -- Here
_2d_arr VARCHAR[] := ARRAY[
['one', 'two', 'three'],
['four', 'five', 'six']
];
BEGIN -- ↓ ↓ ↓ ↓
FOREACH temp SLICE 0 IN ARRAY _2d_arr LOOP
-- RAISE INFO '%', array_to_json(temp);
RAISE INFO '%', temp;
END LOOP;
END
$$;
Then, you can get each element from the 2D array as shown below:
INFO: one
INFO: two
INFO: three
INFO: four
INFO: five
INFO: six
DO
select the_column -> 0 from ...