在PHP中通过字符串模拟$\u POST数组

在PHP中通过字符串模拟$\u POST数组,php,arrays,string,Php,Arrays,String,假设我无法成功访问$\u POST数组,但我确实有一个字符串,该字符串包含所有具有这些值的字段 $string = "x_field1=Value1;x_field2=Value2;x_field3=Value3;x_field4=Value4;" 有没有办法将其转换为PHP$\u POST的格式 array { x_field1 => Value1 x_field2 => Value2 ... } 要模拟$\u POST的确切数据结构,以便传递给第三方API以获得响应?我可以

假设我无法成功访问$\u POST数组,但我确实有一个字符串,该字符串包含所有具有这些值的字段

$string = "x_field1=Value1;x_field2=Value2;x_field3=Value3;x_field4=Value4;"
有没有办法将其转换为PHP$\u POST的格式

array { 
x_field1 => Value1
x_field2 => Value2
...
}
要模拟
$\u POST
的确切数据结构,以便传递给第三方API以获得响应?我可以访问PHP5的某些位,因此我不能使用标准的
$\u POST
,我只能访问字符串,其中“;”作为传递的每个字段的分隔符,“=”作为字段值的分隔符

我尝试了
分解
打开”;“但是这给了我错误的结果,我然后尝试
内爆
第一次分解返回的数组,然后分解开=这也不能让我接近

-解析查询字符串

文档页面中的示例:

$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str, $output);

echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
简单实用的包装器:

class Struct implements Iterator, ArrayAccess, Countable
{
    public $items = array ();
    protected $_strict = true;

    public function __construct ($strict = true) {
        $this->_strict = $strict;
    }

    public function setStrict ($value) {
        $this->_strict = $value;
    }

    public function assign ($data) {
        if (is_array ($data))
            $this->items = @array_change_key_case ($data, CASE_LOWER);
        elseif (is_a ($data, 'Struct'))
            $this->items = $data->items;
    }

    public function append ($data) {
        if (is_array ($data))
            $this->items += @array_change_key_case ($data, CASE_LOWER);
        elseif (is_a ($data, 'Struct'))
            $this->items += $data->items;
    }

    public function merge ($data) {
        if (is_array ($data))
            $this->items = array_merge ($this->items, @array_change_key_case ($data, CASE_LOWER));
        elseif (is_a ($data, 'Struct'))
            $this->items = array_merge ($this->items, $data->items);
    }

    public function clear () {
        $this->items = array ();
    }

    public function exists ($item) {
        return isset ($this->items [strtolower ($item)]);
    }

    public function remove ($item) {
        $field = strtolower ($item);
        if (isset ($this->items [$field])) unset ($this->items [$field]);
    }

    protected function _getValue ($item) {
        $field = strtolower ($item);
        $exists = isset ($this->items [$field]);
        if ($this->_strict && !$exists) throw new OutOfBoundsException ('Struct: Field "' . $item . '" not found');
        return $exists ? $this->items [$field] : null;
    }

    public function __get ($item) {
        return $this->_getValue ($item);
    }

    public function __set ($item, $value) {
        $this->items [strtolower ($item)] = $value;
    }

    public function rewind () {
        reset ($this->items);
    }

    public function current () {
        return current ($this->items);
    }

    public function key () {
        return key ($this->items);
    }

    public function next () {
        return next ($this->items);
    }

    public function valid () {
        return $this->current () !== false;
    }

    public function offsetExists ($offset) {
        return isset ($this->items [strtolower ($offset)]);
    }

    public function offsetGet ($item) {
        return $this->_getValue ($item);
    }

    public function offsetSet ($offset, $value) {
        $this->items [strtolower ($offset)] = $value;
    }

    public function offsetUnset ($offset) {
        $field = strtolower ($ofset);
        if (isset ($this->items [$field])) unset ($this->items [$field]);
    }

    public function count () {
        return count ($this->items);
    }

    public function get ($item, $default = null) {
        return $this->exists ($item) ? $this->$item : $default;
    }
}

// Use it as POST wrapper
$post = new Struct ($_POST);
var_dump ($post->var1);

// Or just any other array
parse_str ('first=value&arr[]=foo+bar&arr[]=baz', $output);
$myPost = new Struct ($output);
var_dump ($myPost->first);
try
{
    var_dump ($myPost->nonexistent_field);
}
catch (OutOfBoundsException $e)
{
    echo "Exception catched! " . $e->getMessage ();
}
$myPost->setStrict (false);
var_dump ($myPost->nonexistent_field);
请注意,
parse_str()
无法识别
作为查询字符串分隔符,必须执行
str_替换(“;”、“&”、$str)在解析它之前。