string(12) "1111" ["resourceCode"] => string(13) "222">
当前位置:Gxlcms > PHP教程 > php数组字符,转换数组解决方法

php数组字符,转换数组解决方法

时间:2021-07-01 10:21:17 帮助过:14人阅读

php 数组字符,转换数组
下面是数组输出后的字符串。我想把它转换成数组
array(9) {
["productCode"] => string(12) "1111"
["resourceCode"] => string(13) "222"
["userCode"] => string(9) "333"
["tvCode"] => string(8) "444"
["extraInfo"] => string(4) "null"
["orderResult"] => string(1) "1"
["subId"] => string(2) "-1"
["payment"] => string(1) "1"
["_URL_"] => array(2) {
[0] => string(3) "test"
[1] => string(14) "callback"
}
}

eval('$dd='.$result.';');
但是一直报错。汗。

分享到:


------解决方案--------------------
你这个串是怎么得来的。是用var_dump输出的吧,最好贴出全部代码以供分析。
------解决方案--------------------
$s =<<< TXT
array(9) {
["productCode"] => string(12) "1111"
["resourceCode"] => string(13) "222"
["userCode"] => string(9) "333"
["tvCode"] => string(8) "444"
["extraInfo"] => string(4) "null"
["orderResult"] => string(1) "1"
["subId"] => string(2) "-1"
["payment"] => string(1) "1"
["_URL_"] => array(2) {
[0] => string(3) "test"
[1] => string(14) "callback"
}
}
TXT;
$s = preg_replace_callback('/(\[
------解决方案--------------------
\]
------解决方案--------------------
string
------解决方案--------------------
\(\d+\))
------解决方案--------------------
({
------解决方案--------------------
}
------解决方案--------------------
"\s+)/', 'foo', $s);
$ar = eval('return ' . trim($s, ',') . ';');
print_r($ar);

function foo($r) {
if($r[1]) return '';
if($r[2] == '{') return '(';
if($r[2] == '}') return '),';
return '",'.PHP_EOL;
}
Array
(
[productCode] => 1111
[resourceCode] => 222
[userCode] => 333
[tvCode] => 444
[extraInfo] => null
[orderResult] => 1
[subId] => -1
[payment] => 1
[_URL_] => Array
(
[0] => test
[1] => callback
)

)

人气教程排行