PHP serialization functions serialize() and unserialize() compared with native functions_PHP tutorial

WBOY
Release: 2016-07-13 09:54:32
Original
1029 people have browsed it

Comparison of php serialization functions serialize() and unserialize() with native functions

This article mainly introduces the php serialization functions serialize() and unserialize() and php native functions For comparison of serialization methods, friends in need can refer to it.

There is a good way in PHP to format strings and convert them into arrays or objects, that is, serialization.

There are two ways to serialize variables.

 The following example uses the serialize() and unserialize() functions:

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

// 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

)

*/

1

2

3

4

5

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

// 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

)

*/

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
// 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 ) */
 This is the native PHP serialization method. However, due to the popularity of JSON in recent years, support for the JSON format has been added to PHP5.2. Now you can use json_encode() and json_decode() functions:  ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // 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 ) */

This will be more efficient and especially compatible with JavaScript and many other languages.

NOTE: For complex objects, some information may be lost.

The above is the entire content of this article, I hope you all like it.

 Note< >: For more exciting tutorials, please pay attention to Bangke Home Programming

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/996760.htmlTechArticlephp serialization functions serialize() and unserialize() compared with native functions This article mainly introduces php serialization Comparing the functions serialize() and unserialize() with PHP's native serialization method, there are...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template