如何在PHP中正确序列化和反序列化数组?

$array = array(
    'host_name' => array(
        'ip_add' => '127.0.0.1',
        'is_allow' => 0,
    ),
);
$str = serialize($array);

This value insert in my website. and read it using php function file_get_contents, I get this result from that page

a:1:{s:9:"host_name";a:2:{s:6:"ip_add";s:9:"127.0.0.1";s:8:"is_allow";i:0;}}

and try to unserialize it, but it show a notice like:-

Notice: unserialize(): Error at offset 5 of 116 bytes in /Applications/XAMPP/xamppfiles/htdocs/admin

If you want unserialize you can use something like this:

<?php

$result = 'a:1:{s:9:"host_name";a:2:{s:6:"ip_add";s:9:"127.0.0.1";s:8:"is_allow";i:0;}}';

$decoded = unserialize($result);

print_r($decoded);

?>

Is the same that:

<?php

$array = array('host_name' => array('ip_add' => '127.0.0.1', 'is_allow' => 0));
$str = serialize($array);

$decoded = unserialize($str);

print_r($decoded);
?>