2011-02-12 3 views
0

$ _POST 배열에 성공적으로 액세스 할 수 없다고 가정하지만 거기에 값이있는 모든 필드가있는 문자열을 사용할 수 있습니다.

$string = "x_field1=Value1;x_field2=Value2;x_field3=Value3;x_field4=Value4;" 

PHP $ _POST와 같은 형식으로 만들 수 있습니까?

array { 
x_field1 => Value1 
x_field2 => Value2 
... 
} 

는 응답을 제 3 자 API에 전달 $_POST의 정확한 데이터 구조를 모방하려면? PHP5의 특정 비트에 액세스 할 수 있으므로 표준을 사용할 수 없습니다. $_POST, ";"과 함께 문자열에만 액세스 할 수 있습니다. 전달 된 각 필드에 대한 구분 기호로 사용되며 "="는 필드 값에 대한 구분 기호로 사용됩니다.

";"에 explode을 시도했습니다. 하지만 그건 내게 잘못된 결과를 준다, 나는 그때 배열을 첫 번째 폭발에 대한 반환하고 다음에 폭발 = 또한 나에게 가까이 가지 않습니다.

답변

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

주,'쿼리 문자열 구분 기호로, 하나는해야 할 것 'str_replace (';', '&', $ str);'구문 분석을하기 전에. – BoltClock

0

간단한 유용한 래퍼 : parse_str()가``인식하지 못하는 '것을

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);