解析将多维数组转换为支持curl提交的一维数组格式

时间:2022-06-10 19:53:21
复制代码 代码如下:


/**
     * @desc    多维数组转化为支持curl提交数组
     * @author    服务器之家    2013-07-8
     */
    public function toPost(array $params = array(), $pre = '')
    {
        $result = array();
        foreach ($params as $key => $val)
        {
                if (is_array($val))
                {
                    $subPre = ($pre=="") ? $key : $pre . "[" . $key . "]";
                    //$pre = "[" . $key . "]";
                    $result = array_merge($result, toPost($val, $subPre));

                }
                else
                {
                    $result[$pre."[".$key."]"] = $val;
                }
        }

 

        return $result;
    }