php三维数组序列化,多维数组序列化








$a = array(
    'a' => 'Apple',
    'b' => 'banana',
    'c' => 'Coconut'
);


$s = serialize($a); // 序列化数组


echo $s;


echo '

'; // 输出结果:a:3:{s:1:"a";s:5:"Apple";s:1:"b";s:6:"banana";s:1:"c";s:7:"Coconut";}


$o = unserialize($s); // 反序列化


print_r($o);


foreach ($o as $key => $value) {
    
    echo $key . '为' . $value . '
';
}
echo '

';
$er3 = 'a:3:{s:1:"a";s:5:"Apple";s:1:"b";s:6:"banana";s:1:"c";s:7:"Coconut";}';
$o3 = unserialize($er3);
print_r($o3);


echo '

';
$er = 'a:1:{s:12:"product_attr";a:2:{i:53;a:2:{s:5:"label";s:6:"颜色";s:5:"value";s:6:"紫色";}i:54;a:2:{s:5:"label";s:6:"尺码";s:5:"value";s:3:"XXL";}}}';
$ok = unserialize($er);
print_r($ok);
foreach ($ok as $key => $value) {
    echo $key . '为' . $value;
}
// ////////////////////////////
echo '

';
echo '

';
$arr_person = array(
    'Tom' => array(
        'phone' => 32523543,
        'address' => 'Hongkong,CHINA',
        'sex' => 'M'
    ),
    'Mary' => array(
        'phone' => 34563643,
        'address' => 'Shengzheng,CHINA',
        'sex' => 'F'
    )
);
$er = 'a:1:{s:12:"product_attr";a:2:{i:53;a:2:{s:5:"label";s:6:"颜色";s:5:"value";s:6:"紫色";}i:54;a:2:{s:5:"label";s:6:"尺码";s:5:"value";s:3:"XXL";}}}';
$ok = unserialize($er);
print_r($ok);
foreach ($ok as $key => $val) {
    foreach ($val as $key1 => $val1) {
        foreach ($val1 as $key2 => $val2) {
            print "
" . $key2 . "=>" . $val2;
        }
    }
}


?>




http://www.palmdy.com

你可能感兴趣的:(php)