如何在php中将以下字符串解析为json

时间:2016-05-04 08:43:55

标签: php json yii yii2

我需要小keyurl作为值,例如: -

  

small-> upload \ 2016 \ 04 \ greenfield-100x100.jpg

"a:3:{s:5:\"small\";s:39:\"\/uploads\/2016\/04\/greenfield-100x100.jpg\";s:6:\"medium\";s:39:\"\/uploads\/2016\/04\/greenfield-300x200.jpg\";s:5:\"large\";s:39:\"\/uploads\/2016\/04\/greenfield-500x400.jpg\";}"" 

1 个答案:

答案 0 :(得分:1)

您需要使用unserialize将数据设为可读/可理解。您的数据有效。

  

unserialize()接受一个序列化变量并将其转换回来   到PHP值。

$data = "a:3:{s:5:\"small\";s:39:\"/uploads/2016/04/greenfield-100x100.jpg\";s:6:\"medium\";s:39:\"/uploads/2016/04/greenfield-300x200.jpg\";s:5:\"large\";s:39:\"/uploads/2016/04/greenfield-500x400.jpg\";}";
$out = unserialize($data);
print_r($out);

unserialize之后的结果。

Array
(
    [small] => /uploads/2016/04/greenfield-100x100.jpg
    [medium] => /uploads/2016/04/greenfield-300x200.jpg
    [large] => /uploads/2016/04/greenfield-500x400.jpg
)