|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-05-14 06:41 UTC] gwynne@php.net
Description:
------------
The unpack() function returns only associative arrays. In some situations it's advantageous to work with an indexed array instead. For example, this code, parsing a fictional network packet format:
$packet = "\x01\x05\x05\x01\x05\x05\x01";
$values = unpack("Cpadding/nvalue1/Cpadding/nvalue2/Cpadding", $packet);
might be more clear when written this way:
list(, $value1, , $value2, ) = unpack("CnCnC", $packet);
Implementing a fully compatible workaround in userland is at least mildly annoying (as well as slow), and it's pretty simple to add to the engine.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 19 16:00:01 2025 UTC |
The userland workaround is rather trivial though, isn't it? list(, $value1, $value2) = array_values(unpack("CnCnC", $packet)); array_values() is quick, but I guess your performance worry is the needless creation of the associative array in the first place?I was under the impression that it's not safe to depend on the order of values in an associative array, i.e., that array_values(array("a" => "b", "c" => "d")) is free to return array("d", "b") if it likes. Is that untrue in the current engine? And yeah, performance is some question. Especially since I am using unpack for network packets, and the faster the better, though I haven't done any kind of benchmark to see if this is a bottleneck at all.