这里我有我的代码。目前它显示原始,编码原始,然后解码它;现在不知怎的,我需要能够获取我的序列化数组并为其添加另一个值!
有人知道如何使用PHP做到这一点吗?也许有一个Jquery示例,我可以输入另一个数字,它会将该数字添加到数组并序列化它?
最终,序列化数据将存储在MySQL row
中,直到它被发送到用户的浏览器然后被反序列化以使用数据来查找使用这些数字的项目,任何人都有任何想法?
<?php
$array["favoritesid1"] = "546";
$array["favoritesid2"] = "516";
$array["favoritesid3"] = "815";
$array["favoritesid4"] = "234";
$array["favoritesid5"] = "765";
$str = serialize($array);
echo "String Original:";
print $str . "\n<br><br>";
$strenc = urlencode($str);
echo "String Original Encoded:";
print $strenc . "\n<br><br>";
$strunserialized = unserialize(urldecode($strenc));
echo "String Original De-Encoded:";
var_dump($strunserialized);
?>
答案 0 :(得分:2)
非常简单,只要在unserialize()
重新创建序列化数组后对数组进行反序列化,就可以添加一个数组,然后如果你想要那样重新序列化它。
<?php
$array["favoritesid1"] = "546";
$array["favoritesid2"] = "516";
$array["favoritesid3"] = "815";
$array["favoritesid4"] = "234";
$array["favoritesid5"] = "765";
$str = serialize($array);
echo "String Original:";
print $str . "\n<br><br>";
$strenc = urlencode($str);
echo "String Original Encoded:";
print $strenc . "\n<br><br>";
$strunserialized = unserialize(urldecode($strenc));
echo "String Original De-Encoded:";
var_dump($strunserialized);
echo 'Add another occurance';
$strunserialized["favoritesid6"] = "666";
print_r( $strunserialized );
echo 'Re Serialize the array';
$new_serial = serialize($strunserialized);
print $new_serial;
?>