|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54734 unpack() forces use of associative arrays
Submitted: 2011-05-14 06:41 UTC Modified: 2011-05-14 07:27 UTC
From: Assigned:
Status: Open Package: Strings related
PHP Version: 5.3.6 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
46 + 50 = ?
Subscribe to this entry?

 [2011-05-14 06:41 UTC]
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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-05-14 06:59 UTC]
-Status: Open +Status: Feedback
 [2011-05-14 06:59 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?
 [2011-05-14 07:08 UTC]
-Status: Feedback +Status: Open
 [2011-05-14 07:08 UTC]
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.
 [2011-05-14 07:17 UTC]
That has never been true in any PHP version. Right from day one PHP arrays have 
been ordered and you have always been able to rely on that order.
 [2011-05-14 07:27 UTC]
I stand corrected!

That having been said, it's still a bit annoying to have to write the array_values() every time, and it does feel a bit wasteful to create an associative array, then create array from just its values, just to extract it into what's essentially another hash table (the symbol table) again. A comment in ext/standard/pack.c suggests the original author of the function rejected the ordered-array approach because it was error-prone, which now seems a little odd to me given your explanation.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 24 01:01:31 2024 UTC