欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php序列化函數(shù)serialize() 和 unserialize() 與原生函數(shù)對比

 更新時間:2015年05月08日 15:30:57   投稿:hebedich  
這篇文章主要介紹了php序列化函數(shù)serialize() 和 unserialize() 與php原生序列化方法對比,有需要的小伙伴可以參考下。

php中有格式化字符串并轉(zhuǎn)換成數(shù)組或?qū)ο蟮暮梅椒ǎ葱蛄谢幚怼?br /> 有兩種序列化變量的方法。

以下示例,使用 serialize() 和 unserialize() 函數(shù):

// a complex array
$myvar = array(
 'hello',
 42,
 array(1,'two'),
 'apple'
);

// convert to a string
$string = serialize($myvar);

echo $string;
/* prints
a:4:{i:0;s:5:"hello";i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3:"two";}i:3;s:5:"apple";}
*/

// you can reproduce the original variable
$newvar = unserialize($string);

print_r($newvar);
/* prints
Array
(
  [0] => hello
  [1] => 42
  [2] => Array
    (
      [0] => 1
      [1] => two
    )

  [3] => apple
)
*/

這是原生的 PHP 序列化方法。

然而,由于 JSON 近年來大受歡迎,PHP5.2 中已經(jīng)加入了對 JSON 格式的支持。

現(xiàn)在你可以使用 json_encode() 和 json_decode() 函數(shù):

// a complex array
$myvar = array(
 'hello',
 42,
 array(1,'two'),
 'apple'
);

// convert to a string
$string = json_encode($myvar);

echo $string;
/* prints
["hello",42,[1,"two"],"apple"]
*/

// you can reproduce the original variable
$newvar = json_decode($string);

print_r($newvar);
/* prints
Array
(
  [0] => hello
  [1] => 42
  [2] => Array
    (
      [0] => 1
      [1] => two
    )

  [3] => apple
)
*/

這將更為行之有效,尤其與 JavaScript 等許多其他語言兼容。

注意:對于復(fù)雜的對象,某些信息可能會丟失。

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評論