|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 15:00:01 2025 UTC |
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));