In PHP development, it is often necessary to encode and decode data, especially when it comes to network transmission or file storage. json_decode() is a common function to parse JSON data into PHP arrays or objects, while the pack() function can package data into binary strings for easy transmission and storage. This article will explain in detail how to package the array results obtained by json_decode() into binary data using pack() .
json_decode(string $json, bool $assoc = true)
Parses the JSON string, the second parameter is true and returns the associative array, and the object is false .
pack(string $format, mixed $args...)
Pack the parameters into binary strings based on the format characters. The format character determines the type and length of each parameter.
Suppose there is the following JSON data:
{
"id": 123,
"temperature": 36.5,
"status": 1
}
After we use json_decode() to convert it into an array, we hope to use pack() to package it into binary data for convenient network transmission.
<?php
// JSON data
$json = '{"id":123,"temperature":36.5,"status":1}';
// use json_decode Resolve as an associative array
$data = json_decode($json, true);
// Confirm the analysis results
// var_dump($data);
// 假设data格式如下:
// id: 32Unsigned integer (N)
// temperature: Floating point number,32Unit accuracy (f)
// status: 8Unsigned integer (C)
// Notice:pack Not supported 64位Floating point number格式,只能use单精度或双精度(d)
// Choose single precision here(f)
$binary = pack(
'NfC', // Format characters:N - 32Unsigned integers for bit endianness, f - Floating point number, C - 8Unsigned integer
$data['id'],
$data['temperature'],
$data['status']
);
// 输出二进制data的十六进制表示,Easy to view
echo bin2hex($binary);
?>
N represents a 32-bit unsigned big-endian integer (network endian), suitable for transmission and cross-platform.
f represents a 32-bit single-precision floating point number.
C represents an 8-bit unsigned integer.
Here the id is packaged as a network endian integer, temperature is a floating point number, and status is a byte.
In order to verify the correctness of packaging, you can unpack it with the unpack() function:
<?php
// 之前打包后的二进制data
$binary = pack('NfC', 123, 36.5, 1);
// Unpacking,Format characters必须与打包时对应
$unpacked = unpack('Nid/ftemperature/Cstatus', $binary);
print_r($unpacked);
?>
Output:
Array
(
[id] => 123
[temperature] => 36.5
[status] => 1
)
If JSON contains multi-dimensional arrays or strings, you need to design a binary format first. Common practices are:
Fixed-length fields are packaged in a fixed format;
Strings are packaged in length + content, such as pack('N', strlen($str)) . $str ;
Array packaging loop.
Example:
<?php
$json = '{"users":[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]}';
$data = json_decode($json, true);
$binary = '';
foreach ($data['users'] as $user) {
$id = $user['id'];
$name = $user['name'];
$nameLen = strlen($name);
$binary .= pack('N', $id); // id 4byte
$binary .= pack('N', $nameLen); // Name length 4byte
$binary .= $name; // Name string
}
echo bin2hex($binary);
?>
json_decode() parses JSON strings into PHP arrays;
pack() packages data into binary according to custom format;
The format needs to be designed, considering the field length and type, especially the string needs its own length information;
Combined with unpack() to verify the correctness of the data.
For more detailed format descriptions of pack functions, please refer to the official PHP documentation .