我终于想明白了。以机智:
<?php
$post = array();
$post['array1'] = json_encode(array(
'info1' => 'test 1',
'info2' => 'test 2',
'info3' => 'test 3'
));
$post['info4'] = '#';
echo serialize($post);
echo "\n";
var_dump(unserialize(serialize($post)));
?>
http://codepad.org/jaHT6lfW
给出:
a:2:{s:6:"array1";s:52:"{"info1":"test 1","info2":"test 2","info3":"test 3"}";s:5:"info4";s:1:"#";}
array(2) {
["array1"]=>
string(52) "{"info1":"test 1","info2":"test 2","info3":"test 3"}"
["info4"]=>
string(1) "#"
}
所以:
<?php
$post = array();
$post['array1'] = json_encode(array(
'info1' => 'test 1',
'info2' => 'test 2',
'info3' => 'test 3'
));
$post['info4'] = '#';
$serial = serialize($post);
echo "$serial\n";
$unserial = unserialize(serialize($post));
var_dump($unserial);
$unserial['array1'] = json_decode($unserial['array1']);
var_dump($unserial);
?>
http://codepad.org/mpfDLyvH
给出正确的输出。
说真的,在使用数组之后,在将其插入数据库之前,只需对所有内容进行 JSON 编码。$_POST
如果您这样做,您将不必担心这种废话。为了理智起见,您的数据实际上应该只使用每个字段的一种常量类型进行序列化。