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 8408479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:38:34+00:00 2026-06-09T23:38:34+00:00

I’m trying with little success to pass a PHP array to the following function.

  • 0

I’m trying with little success to pass a PHP array to the following function.

$recipients = array();
$recipients['6c2f7451-bac8-4cdd-87ce-45d55d152078'] = 5.00;
$recipients['8c2f3421-bac8-4cdd-87ce-45d55d152074'] = 10.00;

$originator = '5630cc6d-f994-43ff-abec-1ebb74d39a3f';
$params = array($originator, $recipients);

pg_query_params(_conn, 'SELECT widgetallocationrequest($1, $2)', $params);

$results = pg_fetch_all(pg_query_params);
...

The function accepts an array of a custom type:

CREATE TYPE widgetallocationrequest AS (id uuid, amount numeric(13,4));

and enumeraties each item and performs an action:

CREATE FUNCTION distributeWidgets(pid uuid, precipients widgetallocationrequest[])
 RETURNS BOOLEAN AS
 $BODY$
{
FOREACH v_recipient IN ARRAY precipients
LOOP
    ...do something
END LOOP;
}
  RETURN TRUE;
END;
$BODY$
LANGUAGE plpgsql VOLATILE STRICT
COST 100;

***(if the specific code sample contains errors it's only pseudocode, i'm really just looking for the best way to pass a php array to a postgres custom type array as a parameter so it can be enumerated in the postgres function)***

Update:
I’m able to successfully call the function from postgres directly (not from PHP) with the following:

SELECT distributeWidgets('5630cc6d-f994-43ff-abec-1ebb74d39a3f',
ARRAY[('ac747f0e-93d4-43a9-bc5b-09df06593239', '5.00'), ('8c2f3421-bac8-4cdd-87ce-45d55d152074', '10.00')]::widgetallocationrequest[]);

But still not sure how to translate from this postgres example back into PHP

I’ve tried suggestions below and the output from the referenced functions yields the following error:

enter image description here

string from function is as follows:

'SELECT account.hldtoexpalloc('0d6311cc-0d74-4a32-8cf9-87835651e1ee', '0124a045-b2e8-4a9f-b8c4-43b1e4cf638d', '{{\"6c2f7451-bac8-4cdd-87ce-45d55d152078\",5.00},{\"8c2f3421-bac8-4cdd-87ce-45d55d152074\",10.00}}')'
  • 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-09T23:38:35+00:00Added an answer on June 9, 2026 at 11:38 pm

    UPDATE: I just noticed that you don’t just need arrays, you need to use arrays of composite types. Ick. I’ve never needed to work with them, so I had to do a bit of checking.

    It seems that the correct PostgreSQL syntax for an array of widgetallocationrequest would be:

    '{"(8c2f3421-bac8-4cdd-87ce-45d55d152074,10.0000)","(6c2f7451-bac8-4cdd-87ce-45d55d152078,5.0000)"}'::widgetallocationrequest[]
    

    See how each composite-type row is enclosed in "(col1,col2)" within the array {a,b,c} container?

    Here’s a PostgreSQL SQL example of how I created the value:

    -- Create the array of composites from a VALUES() statement
    --
    SELECT array_agg(x::widgetallocationrequest) 
    FROM (VALUES 
        ('8c2f3421-bac8-4cdd-87ce-45d55d152074',10.00),
        ('6c2f7451-bac8-4cdd-87ce-45d55d152078',5.00)
    ) x;
    

    … and how I verified it was valid:

    -- Unpack it back into a row-set of columns
    SELECT * FROM unnest('{"(8c2f3421-bac8-4cdd-87ce-45d55d152074,10.0000)","(6c2f7451-bac8-4cdd-87ce-45d55d152078,5.0000)"}'::widgetallocationrequest[]);
    

    Now, PHP’s driver for Pg doesn’t even support arrays, let alone arrays of composite types, so you’re going to have to find someone else who wrote what you want or write it yourself. Writing a reliable parser will be “fun” and not a productive use of time.

    Let’s take another approach: produce a query that lets you call the function sanely by doing the conversion to a widgetallocationrequest[] inside PostgreSQL.

    Here’s a dummy function with the same arguments as your real one that we’ll use as a call target:

    CREATE OR REPLACE FUNCTION distributeWidgets(pid uuid, precipients widgetallocationrequest[]) RETURNS boolean AS $$
    SELECT 't'::boolean;
    $$ LANGUAGE 'sql';
    

    You can see that it can be called with the array-of-composites syntax that’s giving you so much trouble:

    SELECT distributewidgets(null, '{"(8c2f3421-bac8-4cdd-87ce-45d55d152074,10.0000)","(6c2f7451-bac8-4cdd-87ce-45d55d152078,5.0000)"}');
    

    … but ideally you want to avoid producing anything that horrible from PHP, and the driver is missing important features so it can’t do it for you.

    Instead, you can use a TEMPORARY table to produce the arguments, INSERT each argument row into the table with regular parameterized INSERTs, and then execute a query to execute the function.

    BEGIN;
    
    CREATE TEMPORARY TABLE dw_args ( id uuid, amount numeric(13,4) );
    
    -- Use proper parameterized INSERTs from PHP, this is just an example
    INSERT INTO dw_args(id,amount) VALUES ('8c2f3421-bac8-4cdd-87ce-45d55d152074',10.00);
    INSERT INTO dw_args(id,amount) VALUES ('6c2f7451-bac8-4cdd-87ce-45d55d152078',5.00);
    
    SELECT distributewidgets(null, array_agg(ROW(x.*)::widgetallocationrequest)) 
    FROM dw_args x;
    
    DROP TABLE dw_args;
    
    COMMIT;
    

    WARNING: The following is vulnerable to SQL injection if not handled very carefully. Use the above temp table approach if at all possible. Don’t be bobby‘s next victim; read the PHP docs on SQL injection.

    If for some reason it’s absolutely necessary to run it all in one statement, you can instead produce a query with a VALUES set from PHP and convert that into a widgetallocationrequest[] using a PostgreSQL query. I demonstrated it above, but here’s how to combine it with a call to distributeWidgets(...):

    SELECT distributewidgets(null, array_agg(x::widgetallocationrequest)) 
    FROM (VALUES 
            ('8c2f3421-bac8-4cdd-87ce-45d55d152074',10.00),
            ('6c2f7451-bac8-4cdd-87ce-45d55d152078',5.00)
    ) x;
    

    That’s something you can build pretty easily in PHP using string manipulation, so long as you’re really careful about SQL injection.

    Please use the temporary table approach if at all possible.

    See also PHP array to postgres array

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

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.