我如何获得使用http_build_query()编码的数组的值

时间:2011-11-20 15:31:34

标签: php

我有一个数组

阵列

(
    [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
                )

        )

)

我已经使用http_build_query()来接受CURLOPT_POSTFIELDS

有没有办法可以将它恢复为数组形式?如果没有,我如何使用$ _POST得到每个数组的值?

任何建议都将非常感谢!,提前谢谢!干杯!

2 个答案:

答案 0 :(得分:1)

您无需进行任何转换即可将PHP结构传递给curl。它将直接接受一个数组:

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

和curl将处理编码本身。但是,如果您坚持自己动手,请尝试parse_str()parse_url()

答案 1 :(得分:0)

我认为您正在寻找http_build_strDocs

$str = http_build_str($arr);

该函数负责正确构建一个queryinfo-part,就像PHP可以处理的数组编码一样。

对于您的示例数据:

  

用户%5Bid%5D = 11-11-111&安培;用户%5Bbranch_id%5D =安培;用户%5Blast_name%5D = Doe的安培;用户%5Bfirst_name%5D = JHON&安培;用户%5Bmiddle_name%5D =麦当劳&安培; 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

您需要为该功能安装HTTP extension