unpack()

unpack() — Unpack data from binary string.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Unpacks from a binary string into an array according to the given format.

Signature

unpack(string $format, string $string, int $offset = 0): array|false

Parameters

ParameterTypeWhat it takes
$formatstringSee pack() for an explanation of the format codes.
$stringstringThe packed data.
$offsetintThe offset to begin unpacking from.

Return value

Returns an associative array containing unpacked elements of binary string, or false on failure.

Example

A runnable snippet:

$binarydata = "\x04\x00\xa0\x00"; $array = unpack("cchars/nint", $binarydata); print_r($array);

Output:

Array ( [chars] => 4 [int] => 160 )

Changelog

VersionChange
7.2.0float and double types supports both Big Endian and Little Endian.
7.1.0The optional offset has been added.

See also

pack()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page unpack() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.