php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #5889 pack() does not take arrays
Submitted: 2000-08-01 09:51 UTC Modified: 2014-04-11 17:31 UTC
Votes:28
Avg. Score:4.3 ± 1.0
Reproduced:18 of 18 (100.0%)
Same Version:3 (16.7%)
Same OS:7 (38.9%)
From: tomwk at audiogalaxy dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 4.0.1pl2 OS: Redhat Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tomwk at audiogalaxy dot com
New email:
PHP Version: OS:

 

 [2000-08-01 09:51 UTC] tomwk at audiogalaxy dot com
I have an application that writes data across our network
to another program.  Most of the data consists of integer arrays.  I'm currently doing this in a very simple manner,
using something like this:

for( $i = 0; $i < $numInts; $i++ ) {
  fwrite( $fd, pack( "N", $ints[ $i ] ) );
}

However, this results in a very large number of calls to 
fwrite, which is bad for performance.  I made the routine
about 3 times faster by doing something like this:

fwrite( $fd, pack( "N10" 
  $ints[ $i + 0 ],
  $ints[ $i + 1 ],
  $ints[ $i + 2 ], ///.. and so on, 

However, it would be really great if pack could take an array as one of it's arguments.  I believe this is the way 
Perl behaves, but when I try:

$data = array( 10, 123 );
$buf = pack( "N*", $data );

$valArray = unpack( "N*", $buf );

while( list( $key, $val ) = each( $valArray ) ) {
    echo "$key -> $val\n";
}

the only output I get is:

1 -> 1



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-01 10:22 UTC] stas@php.net
Well, functions args work differently in perl and PHP. Maybe we need some "array" type of pack modifier, because PHP does not convert function arguments to array. Though, you can write a wrapper function using func_get_args() and func_num_args() for this.
 [2004-06-14 10:49 UTC] magnus@php.net
This will not be implemented.
 [2011-03-22 19:00 UTC] joshua at gmail dot com
Yes, it WILL be implemented
 [2014-04-11 13:31 UTC] asakurastar at hotmail dot com
When will it be implemented??
 [2014-04-11 17:31 UTC] requinix@php.net
-Package: Feature/Change Request +Package: *General Issues
 [2014-04-11 17:31 UTC] requinix@php.net
It won't be as described (passing arrays) because there are other ways of doing it.

PHP 5.6+ introduces argument unpacking.
$buf = pack("N*", ...$data)

PHP <5.6 can use call_user_func_array().
$buf = call_user_func_array("pack", array_merge(array("N*"), $data));
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC