Hướng dẫn php serialized to array - php tuần tự hóa thành mảng

(Php 4, Php 5, Php 7, Php 8)

serialize - tạo ra một biểu diễn có thể lưu trữ của một giá trịGenerates a storable representation of a value

Sự mô tả

serialize (hỗn hợp $value): Chuỗi(mixed $value): string

Điều này rất hữu ích để lưu trữ hoặc truyền các giá trị PHP xung quanh mà không mất loại và cấu trúc của chúng.

Để biến chuỗi tuần tự hóa thành một giá trị PHP một lần nữa, hãy sử dụng unserialize ().unserialize().

Thông số

value

Giá trị được tuần tự hóa. serialize () xử lý tất cả các loại, ngoại trừ loại tài nguyên và một số đối tượng (xem ghi chú bên dưới). Bạn thậm chí có thể tuần tự hóa các mảng có chứa các tham chiếu đến chính nó. Tài liệu tham khảo tròn bên trong mảng/đối tượng bạn đang tuần tự cũng sẽ được lưu trữ. Bất kỳ tài liệu tham khảo nào khác sẽ bị mất.serialize() handles all types, except the resource-type and some objects (see note below). You can even serialize() arrays that contain references to itself. Circular references inside the array/object you are serializing will also be stored. Any other reference will be lost.

Khi các đối tượng tuần tự hóa, PHP sẽ cố gắng gọi các hàm thành viên __serialize () hoặc __s ngủ () trước khi tuần tự hóa. Điều này là để cho phép đối tượng thực hiện bất kỳ việc làm sạch vào phút cuối, vv trước khi được tuần tự hóa. Tương tự như vậy, khi đối tượng được khôi phục bằng cách sử dụng hàm thành viên unserialize (), hàm thành viên __unserialize () hoặc __wakeup () được gọi.unserialize() the __unserialize() or __wakeup() member function is called.

Ghi chú::

Các thành viên riêng của đối tượng có tên lớp được chuẩn bị cho tên thành viên; Các thành viên được bảo vệ có một '*' được chuẩn bị cho tên thành viên. Các giá trị được chuẩn bị này có byte null ở hai bên.

Trả về giá trị

Trả về một chuỗi chứa biểu diễn dòng byte của value có thể được lưu trữ ở bất cứ đâu.

Lưu ý rằng đây là một chuỗi nhị phân có thể bao gồm các byte null và cần được lưu trữ và xử lý như vậy. Ví dụ: đầu ra serialize () thường được lưu trữ trong trường blob trong cơ sở dữ liệu, thay vì trường char hoặc văn bản.serialize() output should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field.

Ví dụ

Ví dụ #1 serialize () Ví dụserialize() example

// $session_data contains a multi-dimensional array with session
// information for the current user.  We use serialize() to store
// it in a database at the end of the request.
$conn odbc_connect("webdb""php""chicken");
$stmt odbc_prepare($conn,
      
"UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!
odbc_execute($stmt$sqldata)) {
    
$stmt odbc_prepare($conn,
     
"INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!
odbc_execute($stmtarray_reverse($sqldata))) {
        
/* Something went wrong.. */
    
}
}
?>

Ghi chú

Ghi chú::

Các thành viên riêng của đối tượng có tên lớp được chuẩn bị cho tên thành viên; Các thành viên được bảo vệ có một '*' được chuẩn bị cho tên thành viên. Các giá trị được chuẩn bị này có byte null ở hai bên.Serializable interface or the magic __serialize()/__unserialize() or __sleep()/__wakeup() methods. If an internal class does not fulfill any of those requirements, it cannot reliably be serialized.

Trả về giá trị

Trả về một chuỗi chứa biểu diễn dòng byte của value có thể được lưu trữ ở bất cứ đâu.

Lưu ý rằng đây là một chuỗi nhị phân có thể bao gồm các byte null và cần được lưu trữ và xử lý như vậy. Ví dụ: đầu ra serialize () thường được lưu trữ trong trường blob trong cơ sở dữ liệu, thay vì trường char hoặc văn bản.serialize() serializes objects, the leading backslash is not included in the class name of namespaced classes for maximum compatibility.

Ví dụ

  • Ví dụ #1 serialize () Ví dụ
  • Ghi chú
  • Lưu ý rằng nhiều đối tượng PHP tích hợp không thể được tuần tự hóa. Tuy nhiên, những người có khả năng này hoặc thực hiện giao diện có thể nối tiếp hoặc ma thuật __serialize ()/__ unserialize () hoặc __s ngủ ()/__ wakeUp (). Nếu một lớp nội bộ không đáp ứng bất kỳ yêu cầu nào trong số đó, nó không thể được tuần tự hóa một cách đáng tin cậy.
  • Có một số ngoại lệ lịch sử đối với quy tắc trên, trong đó một số đối tượng bên trong có thể được tuần tự hóa mà không thực hiện giao diện hoặc phơi bày các phương thức.
  • __sleep()
  • __wakeup()
  • __serialize()
  • __unserialize()

Cảnh báo

Khi tuần tự hóa () tuần tự hóa các đối tượng, dấu gạch chéo ngược hàng đầu không được bao gồm trong tên lớp của các lớp theo tên để tương thích tối đa.

/*
Anatomy of a serialize()'ed value:

String
s:size:value;

Integer
i:value;

Boolean
b:value; (does not store "true" or "false", does store '1' or '0')

Null
N;

Array
a:size:{key definition;value definition;(repeated per element)}

value0

value1

value2

Xem thêm

unserialize () - Tạo giá trị PHP từ biểu diễn được lưu trữ

value3

value4

value5

value2

var_export () - đầu ra hoặc trả về biểu diễn chuỗi có thể phân tích được của một biến

json_encode () - Trả về biểu diễn JSON của một giá trị

value7

value8

value9

value0

value1

value2

value3

value4

Các đối tượng tuần tự

Egingell tại Sisna Dot Com ¶

value5

value6

value7

value2

16 năm trước

Khi tuần tự hóa () tuần tự hóa các đối tượng, dấu gạch chéo ngược hàng đầu không được bao gồm trong tên lớp của các lớp theo tên để tương thích tối đa.

value9

// $session_data contains a multi-dimensional array with session
// information for the current user.  We use serialize() to store
// it in a database at the end of the request.
$conn odbc_connect("webdb""php""chicken");
$stmt odbc_prepare($conn,
      
"UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!
odbc_execute($stmt$sqldata)) {
    
$stmt odbc_prepare($conn,
     
"INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!
odbc_execute($stmtarray_reverse($sqldata))) {
        
/* Something went wrong.. */
    
}
}
?>
0

// $session_data contains a multi-dimensional array with session
// information for the current user.  We use serialize() to store
// it in a database at the end of the request.
$conn odbc_connect("webdb""php""chicken");
$stmt odbc_prepare($conn,
      
"UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!
odbc_execute($stmt$sqldata)) {
    
$stmt odbc_prepare($conn,
     
"INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!
odbc_execute($stmtarray_reverse($sqldata))) {
        
/* Something went wrong.. */
    
}
}
?>
1

// $session_data contains a multi-dimensional array with session
// information for the current user.  We use serialize() to store
// it in a database at the end of the request.
$conn odbc_connect("webdb""php""chicken");
$stmt odbc_prepare($conn,
      
"UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!
odbc_execute($stmt$sqldata)) {
    
$stmt odbc_prepare($conn,
     
"INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!
odbc_execute($stmtarray_reverse($sqldata))) {
        
/* Something went wrong.. */
    
}
}
?>
2

// $session_data contains a multi-dimensional array with session
// information for the current user.  We use serialize() to store
// it in a database at the end of the request.
$conn odbc_connect("webdb""php""chicken");
$stmt odbc_prepare($conn,
      
"UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!
odbc_execute($stmt$sqldata)) {
    
$stmt odbc_prepare($conn,
     
"INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!
odbc_execute($stmtarray_reverse($sqldata))) {
        
/* Something went wrong.. */
    
}
}
?>
3

// $session_data contains a multi-dimensional array with session
// information for the current user.  We use serialize() to store
// it in a database at the end of the request.
$conn odbc_connect("webdb""php""chicken");
$stmt odbc_prepare($conn,
      
"UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!
odbc_execute($stmt$sqldata)) {
    
$stmt odbc_prepare($conn,
     
"INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!
odbc_execute($stmtarray_reverse($sqldata))) {
        
/* Something went wrong.. */
    
}
}
?>
4

// $session_data contains a multi-dimensional array with session
// information for the current user.  We use serialize() to store
// it in a database at the end of the request.
$conn odbc_connect("webdb""php""chicken");
$stmt odbc_prepare($conn,
      
"UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!
odbc_execute($stmt$sqldata)) {
    
$stmt odbc_prepare($conn,
     
"INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!
odbc_execute($stmtarray_reverse($sqldata))) {
        
/* Something went wrong.. */
    
}
}
?>
5

value2

Xem thêm

unserialize () - Tạo giá trị PHP từ biểu diễn được lưu trữ

// $session_data contains a multi-dimensional array with session
// information for the current user.  We use serialize() to store
// it in a database at the end of the request.
$conn odbc_connect("webdb""php""chicken");
$stmt odbc_prepare($conn,
      
"UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!
odbc_execute($stmt$sqldata)) {
    
$stmt odbc_prepare($conn,
     
"INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!
odbc_execute($stmtarray_reverse($sqldata))) {
        
/* Something went wrong.. */
    
}
}
?>
7

// $session_data contains a multi-dimensional array with session
// information for the current user.  We use serialize() to store
// it in a database at the end of the request.
$conn odbc_connect("webdb""php""chicken");
$stmt odbc_prepare($conn,
      
"UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!
odbc_execute($stmt$sqldata)) {
    
$stmt odbc_prepare($conn,
     
"INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!
odbc_execute($stmtarray_reverse($sqldata))) {
        
/* Something went wrong.. */
    
}
}
?>
8

value2

var_export () - đầu ra hoặc trả về biểu diễn chuỗi có thể phân tích được của một biến

json_encode () - Trả về biểu diễn JSON của một giá trị

/*
Anatomy of a serialize()'ed value:
0

/*
Anatomy of a serialize()'ed value:
1

value2

Các đối tượng tuần tự

Egingell tại Sisna Dot Com ¶

/*
Anatomy of a serialize()'ed value:
3

/*
Anatomy of a serialize()'ed value:
4

/*
Anatomy of a serialize()'ed value:
5

/*
Anatomy of a serialize()'ed value:
6

/*
Anatomy of a serialize()'ed value:
7

/*
Anatomy of a serialize()'ed value:
8

16 năm trước

unserialize () - Tạo giá trị PHP từ biểu diễn được lưu trữ

/*
Anatomy of a serialize()'ed value:
9

var_export () - đầu ra hoặc trả về biểu diễn chuỗi có thể phân tích được của một biến

unserialize () - Tạo giá trị PHP từ biểu diễn được lưu trữ

String
s:size:value;
0

String
s:size:value;
1

var_export () - đầu ra hoặc trả về biểu diễn chuỗi có thể phân tích được của một biến

json_encode () - Trả về biểu diễn JSON của một giá trị

Các đối tượng tuần tự

String
s:size:value;
3

String
s:size:value;
4

String
s:size:value;
5

String
s:size:value;
6

String
s:size:value;
7

String
s:size:value;
8

String
s:size:value;
9

Integer
i:value;
0

Integer
i:value;
1

value2

Xem thêm

unserialize () - Tạo giá trị PHP từ biểu diễn được lưu trữ

Integer
i:value;
3

var_export () - đầu ra hoặc trả về biểu diễn chuỗi có thể phân tích được của một biến

Khi tuần tự hóa () tuần tự hóa các đối tượng, dấu gạch chéo ngược hàng đầu không được bao gồm trong tên lớp của các lớp theo tên để tương thích tối đa.

Integer
i:value;
4

Integer
i:value;
5

Integer
i:value;
6

Integer
i:value;
7

Xem thêm

unserialize () - Tạo giá trị PHP từ biểu diễn được lưu trữ

Integer
i:value;
8

Integer
i:value;
9

Boolean
b:value; (does not store "true" or "false", does store '1' or '0')
0

var_export () - đầu ra hoặc trả về biểu diễn chuỗi có thể phân tích được của một biến

json_encode () - Trả về biểu diễn JSON của một giá trị

Boolean
b:value; (does not store "true" or "false", does store '1' or '0')
1

Boolean
b:value; (does not store "true" or "false", does store '1' or '0')
2

Boolean
b:value; (does not store "true" or "false", does store '1' or '0')
3

Boolean
b:value; (does not store "true" or "false", does store '1' or '0')
4

Boolean
b:value; (does not store "true" or "false", does store '1' or '0')
5

Boolean
b:value; (does not store "true" or "false", does store '1' or '0')
6

Các đối tượng tuần tự

Egingell tại Sisna Dot Com ¶

Boolean
b:value; (does not store "true" or "false", does store '1' or '0')
7

Boolean
b:value; (does not store "true" or "false", does store '1' or '0')
8

value2

16 năm trước

Các đối tượng tuần tự

Null
N;
0

Null
N;
1

value2

Egingell tại Sisna Dot Com ¶

16 năm trước

Null
N;
3

Null
N;
4

Null
N;
5

Null
N;
6

Null
N;
7

Null
N;
8

value2

Ẩn danh ¶

unserialize () - Tạo giá trị PHP từ biểu diễn được lưu trữ

Array
a:size:{key definition;value definition;(repeated per element)}
0

Array
a:size:{key definition;value definition;(repeated per element)}
1

Array
a:size:{key definition;value definition;(repeated per element)}
2

Array
a:size:{key definition;value definition;(repeated per element)}
3

var_export () - đầu ra hoặc trả về biểu diễn chuỗi có thể phân tích được của một biến

json_encode () - Trả về biểu diễn JSON của một giá trị

Array
a:size:{key definition;value definition;(repeated per element)}
4

Array
a:size:{key definition;value definition;(repeated per element)}
5

value2

Xem thêm

16 năm trước

Array
a:size:{key definition;value definition;(repeated per element)}
7

Array
a:size:{key definition;value definition;(repeated per element)}
8

Array
a:size:{key definition;value definition;(repeated per element)}
9

value00

value01

value02

Ẩn danh ¶

Egingell tại Sisna Dot Com ¶

value03

value04

value05

value2

16 năm trước

16 năm trước

value07

value08

value09

value2

Ẩn danh ¶

Khi tuần tự hóa () tuần tự hóa các đối tượng, dấu gạch chéo ngược hàng đầu không được bao gồm trong tên lớp của các lớp theo tên để tương thích tối đa.

value11

value12

value13

value14

value15

value16

value17

value2

Xem thêm

unserialize () - Tạo giá trị PHP từ biểu diễn được lưu trữ

value19

value20

value2

var_export () - đầu ra hoặc trả về biểu diễn chuỗi có thể phân tích được của một biến

11 năm trước

value22

Markroedel tại Letu Dot Edu ¶

22 năm trước

value23

allan666 tại gmail.com ¶

9 năm trước

value24

value25

value26

value27

value28

value29

value2

donotmail tại tôi dot com ¶

13 năm trước

value31

value32

value33

value34

value35

value36

Thứ Sáu13 tại IG Dot Com Dot Br ¶

15 năm trước

value37

value38

value39

value40

value41

value42

value2

tuxedobob ¶

8 năm trước

value44

value45

value46

value47

value48

value2