下面是我的字符串,直接从数据库取出来的,如何才可以转换成相应的数组?
Array
(
[currency] => USD
[shipping] => 0
[tax] => 0
[taxRate] => 0
[itemCount] => 2
[item_name_1] => NO: 35804
[item_quantity_1] => 2
[item_price_1] => 228
[item_options_1] => link: ./index.php?id=35804
[item_name_2] => NO: 35802
[item_quantity_2] => 1
[item_price_2] => 228
[item_options_2] => link: ./index.php?id=35802
)
Array
(
[currency] => USD
[shipping] => 0
[tax] => 0
[taxRate] => 0
[itemCount] => 2
[item_name_1] => NO: 35804
[item_quantity_1] => 2
[item_price_1] => 228
[item_options_1] => link: ./index.php?id=35804
[item_name_2] => NO: 35802
[item_quantity_2] => 1
[item_price_2] => 228
[item_options_2] => link: ./index.php?id=35802
)
$arr = array(
'currency' => 'USD',
'shipping' => 0,
'tax' => 0,
'taxRate' => 0,
'itemCount' => 2,
'item_name_1' => 'NO: 35804',
'item_quantity_1' => 2,
'item_price_1' => 228,
'item_options_1' => 'link: ./index.php?id=35804',
'item_name_2' => 'NO: 35802',
'item_quantity_2' => 1,
'item_price_2' => 228,
'item_options_2' => 'link: ./index.php?id=35802'
);print_r($arr);
* 类 unprint_r 继承于 Trie
* 功能: 将 php 函数 print_r 产生的结果还原成 php 的数组
**/
class unprint_r extends Trie {
public $res = array(); //还原后的数组
public $stack = array(); //工作栈
protected $keyname = '__root__'; //键名缓存
protected $deep = 0; function __construct($str='') {
$this->set('Array', 'group')
->set('array', 'group')
->set(' [', 'brackets,[')
->set('[', 'brackets,[')
->set(" ['", 'brackets,[')
->set("['", 'brackets,[')
->set('] => ', 'brackets,]')
->set('] =>', 'brackets,]')
->set(']=>', 'brackets,]')
->set("'] => ", 'brackets,]')
->set("'] =>", 'brackets,]')
->set("']=>", 'brackets,]')
->set(')', 'brackets,)'); if($str) $this->parse($str);
} /**
* 方法: group
* 功能: 处理数组开始部分
**/
protected function group() {
array_unshift($this->stack, array( 'key' => $this->keyname, 'value' => array() ));
$this->keyname = '';
if($this->deep == 0) $this->deep = 1;
else $this->deep += 2;
} /**
* 方法: brackets
* 功能: 处理括号部分
**/
protected function brackets($c) {
switch($c) {
case '[':
case ')':
if($this->keyname != '')
$this->stack[0]['value'][$this->keyname] = trim($this->buf);
$this->keyname = '';
if($c == '[') break;
$t = array_shift($this->stack);
$this->res = $t['value'];
$this->stack[0]['value'][$t['key']] = $t['value'];
break;
case ']':
if($this->keyname == '') $this->keyname = $this->buf;
}
$this->buf = '';
}
}使用$s =<<< TXT
Array
(
[currency] => USD
[shipping] => 0
[tax] => 0
[taxRate] => 0
[itemCount] => 2
[item_name_1] => NO: 35804
[item_quantity_1] => 2
[item_price_1] => 228
[item_options_1] => link: ./index.php?id=35804
[item_name_2] => NO: 35802
[item_quantity_2] => 1
[item_price_2] => 228
[item_options_2] => link: ./index.php?id=35802
)
TXT;
$p = new unprint_r($s);
var_export($p->res);array (
'currency' => 'USD',
'shipping' => '0',
'tax' => '0',
'taxRate' => '0',
'itemCount' => '2',
'item_name_1' => 'NO: 35804',
'item_quantity_1' => '2',
'item_price_1' => '228',
'item_options_1' => 'link: ./index.php?id=35804',
'item_name_2' => 'NO: 35802',
'item_quantity_2' => '1',
'item_price_2' => '228',
'item_options_2' => 'link: ./index.php?id=35802',
)
protected $dict = array();
protected $buf = '';
function set($word, $value='') {
if(is_array($word)) foreach($word as $k=>$v) $this->set($k, $v);
$p =& $this->dict;
foreach(str_split($word) as $ch) {
if(! isset($p[$ch])) $p[$ch] = array();
$p =& $p[$ch];
}
$p['val'] = $value;
return $this;
}
function parse($str) {
$this->doc = $str;
$this->len = strlen($str);
$i = 0;
while($i < $this->len) {
$t = $this->find($this->dict, $i);
if($t) {
$i = $t;
$this->buf = '';
}else $this->buf .= $this->doc{$i++};
}
}
protected function find(&$p, $i) {
if($i >= $this->len) return $i;
$t = 0;
$n = $this->doc{$i};
if( isset($p[$n]) ) $t = $this->find($p[$n], $i+1);
if($t) return $t;
if( isset($p['val']) ) {
$ar = explode(',', $p['val']);
call_user_func_array( array($this, array_shift($ar)), $ar );
return $i;
}
return $t;
}
function __call($method, $param) {
echo "****\n$this->buf 未定义方法:$method 参数:" . join(',', $param) . "<br />\n";
}
}class unprint_r extends Trie {
public $res = array(); //还原后的数组
public $stack = array(); //工作栈
protected $keyname = '__root__'; //键名缓存
protected $deep = 0;
function __construct($str='') {
$this->set('Array', 'group')
->set('array', 'group')
->set(' [', 'brackets,[')
->set('[', 'brackets,[')
->set(" ['", 'brackets,[')
->set("['", 'brackets,[')
->set('] => ', 'brackets,]')
->set('] =>', 'brackets,]')
->set(']=>', 'brackets,]')
->set("'] => ", 'brackets,]')
->set("'] =>", 'brackets,]')
->set("']=>", 'brackets,]')
->set(')', 'brackets,)');
if($str) $this->parse($str);
}
/**
* 方法: group
* 功能: 处理数组开始部分
**/
protected function group() {
array_unshift($this->stack, array( 'key' => $this->keyname, 'value' => array() ));
$this->keyname = '';
if($this->deep == 0) $this->deep = 1;
else $this->deep += 2;
}
/**
* 方法: brackets
* 功能: 处理括号部分
**/
protected function brackets($c) {
switch($c) {
case '[':
case ')':
if($this->keyname != '')
$this->stack[0]['value'][$this->keyname] = trim($this->buf);
$this->keyname = '';
if($c == '[') break;
$t = array_shift($this->stack);
$this->res = $t['value'];
$this->stack[0]['value'][$t['key']] = $t['value'];
break;
case ']':
if($this->keyname == '') $this->keyname = $this->buf;
}
$this->buf = '';
}
}$s =<<< TXT
Array
(
[currency] => USD
[shipping] => 0
[tax] => 0
[taxRate] => 0
[itemCount] => 2
[item_name_1] => NO: 35804
[item_quantity_1] => 2
[item_price_1] => 228
[item_options_1] => link: ./index.php?id=35804
[item_name_2] => NO: 35802
[item_quantity_2] => 1
[item_price_2] => 228
[item_options_2] => link: ./index.php?id=35802
)
TXT;$obj = new unprint_r($s);
$arr = $obj->stack[0]['value'];
var_export($arr);
输出array (
'currency' => 'USD',
'shipping' => '0',
'tax' => '0',
'taxRate' => '0',
'itemCount' => '2',
'item_name_1' => 'NO: 35804',
'item_quantity_1' => '2',
'item_price_1' => '228',
'item_options_1' => 'link: ./index.php?id=35804',
'item_name_2' => 'NO: 35802',
'item_quantity_2' => '1',
'item_price_2' => '228',
)
$obj = new unprint_r($s . ' '); //尾部加个空格