Req #54734 [Opn->Fbk]: unpack() forces use of associative arrays

From: Date: Sat, 14 May 2011 04:59:05 +0000
Subject: Req #54734 [Opn->Fbk]: unpack() forces use of associative arrays
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-160725@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=54734&edit=1 ID: 54734 Updated by: rasmus@php.net Reported by: gwynne@php.net Summary: unpack() forces use of associative arrays -Status: Open +Status: Feedback Type: Feature/Change Request Package: Strings related Operating System: * PHP Version: 5.3.6 Block user comment: N Private report: N New Comment: 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? Previous Comments: ------------------------------------------------------------------------ [2011-05-14 06:41:56] 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. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=54734&edit=1

« previous php.bugs (#160725) next »