如何反序列化两个序列化数组

时间:2013-02-26 05:19:07

标签: php

我在数据库中存储了多个序列化数组。请点击这个

a:2:{s:2:"B5";s:5:"user1";s:2:"B4";s:5:"user1";}a:1:{s:2:"A3";s:5:"user1";}a:1:{s:2:"A4";s:5:"user1";}

但是当我使用

反序列化该数组时
    $booked_seats = $r1['user_seats'];
    $b = unserialize($booked_seats);
    var_dump($b);

它只显示了第一个非序列化数组的结果,

   array
  'B5' => string 'user1' (length=5)
  'B4' => string 'user1' (length=5)

但我希望我的所有数组都是unserialize..pls ...支持....我应该使用什么?

感谢。

2 个答案:

答案 0 :(得分:1)

我们可以使用}爆炸连接的序列化字符串吗?

$r1['user_seats'] = 'a:2:{s:2:"B5";s:5:"user1";s:2:"B4";s:5:"user1";}a:1:{s:2:"A3";s:5:"user1";}a:1:{s:2:"A4";s:5:"user1";}';
$user_seats = explode('}', $r1['user_seats']);
foreach ($user_seats as $user_seat) {
    if (strlen($user_seat)) {
        $user_seat .= '}';
        $booked_seat = $user_seat;
        $b = unserialize($booked_seat);
        var_dump($b);
    }
}

答案 1 :(得分:0)

$serial = 'a:2:{s:2:"B5";s:5:"user1";s:2:"B4";s:5:"user1";}a:1:{s:2:"A3";s:5:"user1";}a:1:{s:2:"A4";s:5:"user1";}';
$serial=str_replace('{', '|', $serial);
$serial=str_replace('}', '|', $serial);

$p = explode('|', $serial);
echo '<pre>';
print_r($p);

$total = count($p);
$arrr=array();
for($i=0; $i<=$total; $i++){
    if(strlen($p[$i])>1){
        $arrr[$i] = $p[$i] . "{" . $p[$i+ 1]  . "}";
        $i++;
    }
}
foreach($arrr as $key => $value){
    var_dump(unserialize($value));
}

结果:

array(2) {
  ["B5"]=>
  string(5) "user1"
  ["B4"]=>
  string(5) "user1"
}
array(1) {
  ["A3"]=>
  string(5) "user1"
}
array(1) {
  ["A4"]=>
  string(5) "user1"
}