0

i have an array

Array

(
    [User] => Array
        (
            [id] => 11-11-111
            [branch_id] => 
            [last_name] => Doe
            [first_name] => Jhon
            [middle_name] => Mcdonald
        )

    [UserOrder] => Array
        (
            [0] => Array
                (
                    [id] => 0-01-66168-21                     
                    [user_id] => 11-11-111
                    [date_requested] => 2011-11-16
                    [time_requested] => 15:39:08
                )

            [1] => Array
                (
                    [id] => 0-03-65692-13
                    [patient_id] => 11-11-111
                    [date_requested] => 2011-11-07
                    [time_requested] => 06:48:08
                )

            [2] => Array
                (
                    [id] => 0-08-56323-24
                    [patient_id] => 11-11-111
                    [date_requested] => 2011-11-07
                    [time_requested] => 07:09:28
                )

        )

)

i have used http_build_query() for this to be accepted in CURLOPT_POSTFIELDS

Is there a way i can revert it back to its array form? if not, how can i get the value of each array using $_POST?

Any suggestion will be very much appreciated!, thank you in advance! cheers!

2 Answers 2

1

You don't need to do any transformations to pass a PHP structure to curl. It will accept an array directly:

 $array = (.... your structure here ...);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $array);

and curl will handle the encoding itself. However, if you insist on doing it yourself, then try parse_str() or parse_url().

Sign up to request clarification or add additional context in comments.

1 Comment

im getting an error like this one "Array to string conversion".
0

I think you're looking for http_build_strDocs:

$str = http_build_str($arr);

The function takes care to properly build a queryinfo-part like encoding of an array as can be dealt with by PHP.

For your example data:

User%5Bid%5D=11-11-111&User%5Bbranch_id%5D=&User%5Blast_name%5D=Doe&User%5Bfirst_name%5D=Jhon&User%5Bmiddle_name%5D=Mcdonald&UserOrder%5B0%5D%5Bid%5D=0-01-66168-21&UserOrder%5B0%5D%5Buser_id%5D=11-11-111&UserOrder%5B0%5D%5Bdate_requested%5D=2011-11-16&UserOrder%5B0%5D%5Btime_requested%5D=15%3A39%3A08&UserOrder%5B1%5D%5Bid%5D=0-03-65692-13&UserOrder%5B1%5D%5Bpatient_id%5D=11-11-111&UserOrder%5B1%5D%5Bdate_requested%5D=2011-11-07&UserOrder%5B1%5D%5Btime_requested%5D=06%3A48%3A08&UserOrder%5B2%5D%5Bid%5D=0-08-56323-24&UserOrder%5B2%5D%5Bpatient_id%5D=11-11-111&UserOrder%5B2%5D%5Bdate_requested%5D=2011-11-07&UserOrder%5B2%5D%5Btime_requested%5D=07%3A09%3A28

You need to have the HTTP extension installed for that function.

4 Comments

how can i get the value of my array using $_POST after performing http_build_str?
Use that string as post body, you have it in $_POST then, like the original array.
is there a way i could revert it back in its original form? (array form)
$_POST is in array form. The string as in the answer can be converted with de.php.net/parse_str as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.