让我们假设我无法成功访问$ _POST数组,但我确实拥有一个包含所有字段的字符串。
$string = "x_field1=Value1;x_field2=Value2;x_field3=Value3;x_field4=Value4;"
有没有办法让它变成与PHP $ _POST相同的格式?
array {
x_field1 => Value1
x_field2 => Value2
...
}
模仿$_POST
的确切数据结构以传递给第三方API以获取响应?我可以访问PHP5的某些位,所以我不能使用标准$_POST
,我只能访问带有“;”的字符串作为每个字段传递的分隔符,“=”作为字段值的分隔符。
我试过explode
on“;”但这给了我错误的结果,然后我尝试implode
为第一次爆炸返回的数组,然后爆炸=也不会让我接近。
答案 0 :(得分:1)
parse_str()
- 解析查询字符串。
文档页面中的示例:
$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
答案 1 :(得分:0)
简单有用的包装器:
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);